2021与蓝度共同重构项目,服务端
liuhaonan
2022-04-14 5ae0033f237e038ad3edf60c3dc6178be599493f
诺瓦更改
已修改2个文件
36 ■■■■■ 文件已修改
sandu-common/src/main/java/com/sandu/common/file/FileUploadDto.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
sandu-common/src/main/java/com/sandu/common/file/impl/AliOssFileServiceImpl.java 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
sandu-common/src/main/java/com/sandu/common/file/FileUploadDto.java
@@ -1,5 +1,6 @@
package com.sandu.common.file;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
/**
@@ -55,4 +56,10 @@
     */
    private Integer width;
    /**
     * md5
     */
    @TableField(exist = false)
    private String md5;
}
sandu-common/src/main/java/com/sandu/common/file/impl/AliOssFileServiceImpl.java
@@ -13,7 +13,6 @@
import com.sandu.common.execption.BusinessException;
import com.sandu.common.file.FileUploadDto;
import com.sandu.common.file.IFileUpload;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@@ -23,6 +22,8 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@@ -48,6 +49,7 @@
        String dayStr = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
        String fileName = dayStr + UUID.fastUUID()+ "." + suffix;
        String md5 = getMD5(multipartFile);
        FileUploadDto uploadDto = new FileUploadDto();
        OSS ossClient = new OSSClientBuilder().build(endPoint, keyId, keySecret);
        try {
@@ -75,6 +77,7 @@
                        url = host + fileName;
                    }
                }
                uploadDto.setFileUrl(url);
            }
        } catch (Exception e) {
@@ -90,6 +93,7 @@
        uploadDto.setFileType(FileUtil.extName(originalFilename));
        uploadDto.setStorageType(FileStorageEnums.ALIBABA.getCode());
        uploadDto.setFileSize(size);
        uploadDto.setMd5(md5);
        if (IMAGE_SUFFIX_LIST.contains(uploadDto.getFileType())) {
            BufferedImage img = null;
@@ -158,4 +162,27 @@
        return uploadDto;
    }
    /**
     * 获取上传文件的MD5值
     *
     * @param file
     * @return
     */
    public String getMD5(MultipartFile file) {
        try {
            byte[] uploadBytes = file.getBytes();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(uploadBytes);
            String hashString = new BigInteger(1, digest).toString(16);
            if (hashString.length() % 2 != 0) {
                hashString = "0" + hashString;
            }
            return hashString;
        } catch (Exception e) {
        }
        return null;
    }
}