UploadUtil.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.backendsys.modules.upload.utils;
  2. import cn.hutool.core.codec.Base64;
  3. import cn.hutool.core.convert.Convert;
  4. import com.backendsys.exception.CustException;
  5. import com.backendsys.modules.common.utils.CommonUtil;
  6. import com.backendsys.modules.upload.entity.SysFileResult;
  7. import com.backendsys.utils.response.Result;
  8. import com.backendsys.utils.response.ResultEnum;
  9. import net.coobird.thumbnailator.Thumbnails;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.stereotype.Component;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import org.springframework.web.util.UriComponentsBuilder;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. import java.util.LinkedHashMap;
  19. import java.util.Map;
  20. @Component
  21. public class UploadUtil {
  22. @Value("${file.upload.directory}") // 配置保存文件的目录
  23. private String FILE_UPLOAD_DIRECTORY;
  24. @Value("${file.upload.url-prefix}") // 指定文件访问的 URL前缀
  25. private String FILE_UPLOAD_URL_PREFIX;
  26. public SysFileResult put(MultipartFile multipartFile) {
  27. try {
  28. // 创建保存文件的目录,如果不存在
  29. File directory = new File(FILE_UPLOAD_DIRECTORY);
  30. if (!directory.exists()) { directory.mkdirs(); }
  31. // 获取文件原始名
  32. String originalFileName = multipartFile.getOriginalFilename();
  33. // 获取后缀名
  34. String suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
  35. // 生成新的文件名 (UUID)
  36. String newFileName = CommonUtil.generateUUIDFilename(multipartFile);
  37. String newFileNameThumb = newFileName.replaceAll(suffix, "-thumb" + suffix);
  38. // 按日期作为文件目录
  39. String pathDir = new SimpleDateFormat("yyyyMMdd").format(new Date());
  40. String dateDir = pathDir + File.separator;
  41. File dateDirectory = new File(FILE_UPLOAD_DIRECTORY + dateDir);
  42. if (!dateDirectory.exists()) { dateDirectory.mkdirs(); }
  43. // 确定文件保存的路径
  44. String sourceFilePath = FILE_UPLOAD_DIRECTORY + dateDir + newFileName;
  45. // String sourceFilePathThumb = FILE_UPLOAD_DIRECTORY + dateDir + newFileNameThumb;
  46. System.out.println(sourceFilePath);
  47. // System.out.println(sourceFilePathThumb);
  48. File sourceFile = new File(sourceFilePath);
  49. // File sourceFileThumb = new File(sourceFilePathThumb);
  50. // 保存文件到本地
  51. multipartFile.transferTo(sourceFile);
  52. // 创建缩略图
  53. // .scale(0.5) // 缩放
  54. // .outputFormat("png") // 类型
  55. // .sourceRegion(Positions.TOP_RIGHT, 1800, 1800) // 裁剪
  56. // .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("watermark.png")), 0.5f) // 添加水印
  57. // .outputQuality(0.2) // 质量
  58. // 文件批量操作
  59. // Thumbnails.of(new File("/images/202401/").listFiles()).size(400, 400).toFiles(Rename.PREFIX_DOT_THUMBNAIL);
  60. // Thumbnails.of(sourceFilePath).size(200, 200).toFile(sourceFilePathThumb);
  61. // 返回文件的路径 (绝对路径)
  62. String fileUrl = FILE_UPLOAD_URL_PREFIX + pathDir + "/" + newFileName;
  63. // String fileThumbUrl = FILE_UPLOAD_URL_PREFIX + pathDir + "/" + newFileNameThumb;
  64. // 自定义返回结果实体
  65. SysFileResult result = new SysFileResult();
  66. result.setDomain(fileUrl);
  67. result.setKey(sourceFilePath);
  68. return result;
  69. } catch (IOException e) {
  70. throw new CustException(e.getMessage());
  71. }
  72. }
  73. public void delete(String object_key) {
  74. // 根据传入的文件路径创建 File 对象
  75. File fileToDelete = new File(object_key);
  76. // 检查文件是否存在
  77. if (fileToDelete.exists()) {
  78. // 删除文件
  79. if (fileToDelete.delete()) {
  80. System.out.println("本地文件删除成功 (" + object_key + ")");
  81. }
  82. } else {
  83. System.out.println("本地文件不存在,删除失败 (" + object_key + ")");
  84. }
  85. }
  86. // 不同的云环境 (target),缩略图配置也不一样
  87. // -1:本地:
  88. // 1:腾讯云: https://cloud.tencent.com/document/product/436/113295
  89. // 2:阿里云:
  90. // 3:抖音云: https://www.volcengine.com/docs/6349/153626
  91. public static String getImageThumbUrl(String url, Integer target, Integer width, Integer height, String backgroundColor) {
  92. if (target == -1) {
  93. UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
  94. if (width != null) builder.queryParam("w", width);
  95. if (height != null) builder.queryParam("h", height);
  96. return builder.toUriString();
  97. // return url + "?" + "w=" + width + "&h=" + height;
  98. }
  99. // 腾讯云 (color值通过base64加密, #f8f8f8)
  100. if (target == 1) {
  101. backgroundColor = "#" + backgroundColor;
  102. String w = (width != null) ? Convert.toStr(width) : "";
  103. String h = (height != null) ? Convert.toStr(height) : "";
  104. return url + "?imageMogr2/thumbnail/" + w + "x" + h + "/pad/1/color/" + Base64.encode(backgroundColor);
  105. }
  106. // 抖音云
  107. if (target == 3) {
  108. String w = (width != null) ? (",w_" + width) : "";
  109. String h = (height != null) ? (",h_" + height) : "";
  110. // return url + "?x-tos-process=image/resize,w_" + width + ",h_" + height + ",m_pad,color_" + backgroundColor;
  111. return url + "?x-tos-process=image/resize" + w + h + ",color_" + backgroundColor;
  112. }
  113. return url;
  114. }
  115. }