| | |
| | | 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; |
| | |
| | | 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; |
| | | |
| | |
| | | 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 { |
| | |
| | | url = host + fileName; |
| | | } |
| | | } |
| | | |
| | | uploadDto.setFileUrl(url); |
| | | } |
| | | } catch (Exception e) { |
| | |
| | | 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; |
| | |
| | | |
| | | 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; |
| | | } |
| | | |
| | | } |