医院废弃物信息化管理平台
“mitao”
2026-04-21 2f18b3a28054c3917010f95ba84f7425280a2622
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cn.besteam.modules.system.service.impl;
 
import cn.besteam.common.constant.ErrorCode;
import cn.besteam.common.constant.OssTypeEM;
import cn.besteam.common.exception.BusinessException;
import cn.besteam.common.util.StringUtil;
import cn.besteam.config.OssConfig;
import cn.besteam.modules.system.service.IOssService;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.ObjectMetadata;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
 
/**
 * @author jacob
 */
@Service("ossService")
@Slf4j
public class OssServiceImpl implements IOssService {
 
    /**
     * 系统文件分割符
     */
    private final String separator = File.separator;
 
    /**
     * oss配置
     */
    @Resource
    private OssConfig ossConfig;
 
    /**
     * 上传客户端
     */
    @Resource
    private OSSClient uploadOSSClient;
 
    /**
     * @param filenameExtension 文件后缀
     * @return 文件的contentType
     * @Describe 判断OSS服务文件上传时文件的contentType
     */
    private static String getContentType(String filenameExtension) {
 
        return OssTypeEM.getByFileType(filenameExtension.toLowerCase()).getContentType();
    }
 
    /**
     * @param file 文件
     * @param file 文件
     * @return 文件访问路径
     * @Describe 上传文件,返回文件访问路径(默认上传到根目录)
     */
    @Override
    public String upload(MultipartFile file) throws BusinessException {
 
        return upload(ossConfig.getFolder(), file);
    }
//    /**
//     * @param file 文件
//     * @param dir 子目录
//     * @return 文件访问路径
//     * @Describe 上传文件,返回文件访问路径(默认上传到根目录+子目录)
//     */
//    public Result upload(MultipartFile file, String dir) throws BusinessException {
//        return upload(ossConfig.getFolder()+separator+dir,file);
//    }
 
    /**
     * @param storagePath 存储的路径
     * @param file        文件
     * @return 文件访问路径
     * @Describe 上传文件,返回文件访问路径
     */
    @Override
    public String upload(String storagePath, MultipartFile file) throws BusinessException {
 
        String originFileName = file.getOriginalFilename();
        String fileExt = originFileName.substring(originFileName.lastIndexOf(".") + 1);
        String fileName = originFileName.substring(0,originFileName.lastIndexOf(".")  );
        // 设置文件名
        String filePathName = generateRelativeStoragePath(storagePath, fileExt,fileName);
        byte[] fileContent = null;
        try {
            fileContent = file.getBytes();
        } catch (Exception e) {
            log.error("Cannot get file content from {}.", originFileName);
            throw new BusinessException(ErrorCode.ERR_500, "不能读取" + originFileName + "内容");
        }
        ObjectMetadata meta = new ObjectMetadata();
        // 设置上传文件长度
        meta.setContentLength(file.getSize());
        // 设置上传MD5校验
        String md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(fileContent));
        meta.setContentMD5(md5);
        meta.setContentType(getContentType(fileExt));
        // 存储
        try {
            uploadOSSClient.putObject(ossConfig.getBucketName(), filePathName, file.getInputStream(), meta);
        } catch (OSSException | ClientException | IOException e) {
            log.error("OSS storage error", e);
            throw new BusinessException(ErrorCode.ERR_600);
        }
        return ossConfig.getDownloadEndpoint() + filePathName;
    }
 
    /**
     * 上传
     *
     * @param imageBytes
     * @return
     */
    @Override
    public String uploadImage(byte[] imageBytes) throws BusinessException {
 
        return uploadImage(ossConfig.getFolder(), imageBytes);
    }
 
    @Override
    public String uploadImage(String storagePath, byte[] imageBytes) throws BusinessException {
 
        String fileType = "image/jpeg";
        // 设置文件名
        String filePathName = generateRelativeStoragePath(storagePath, "jpeg",null);
        ObjectMetadata meta = new ObjectMetadata();
        // 设置上传文件长度
        meta.setContentLength(imageBytes.length);
        // 设置上传MD5校验
        String md5 = BinaryUtil.toBase64String(BinaryUtil.calculateMd5(imageBytes));
        meta.setContentMD5(md5);
        meta.setContentType(fileType);
        // 存储
        try {
            uploadOSSClient.putObject(ossConfig.getBucketName(), filePathName, new ByteArrayInputStream(imageBytes), meta);
        } catch (Exception e) {
            log.error("OSS storage error", e);
            throw new BusinessException(ErrorCode.ERR_600);
        }
        return ossConfig.getDownloadEndpoint() + filePathName;
    }
 
    /**
     * <pre>
     * 获取存储的相对路径
     * 规则path + / + yyyyMMddHH  + uuid
     * </pre>
     *
     * @param storagePath
     * @return
     */
    private String generateRelativeStoragePath(String storagePath, String fileType,String fileName) {
 
        String time = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
        String uuid = StringUtil.uuid();
        StringBuilder sb = new StringBuilder();
        if (StringUtil.isNotBlank(storagePath)) {
            sb.append(storagePath).append("/");
        }
        if(fileName == null){
            sb.append(time).append(uuid);
        }else {
            sb.append(fileName).append(time);
        }
        if (StringUtil.isNotBlank(fileType)) {
            sb.append(".").append(fileType);
        }
        return sb.toString();
    }
 
}