UploadUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.xjrsoft.common.utils;
  2. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  3. import com.xjrsoft.common.constant.GlobalConstant;
  4. import com.xjrsoft.common.exception.MyException;
  5. import com.xjrsoft.common.factory.OssFactory;
  6. import com.xjrsoft.config.FileCheckRuleConfig;
  7. import okhttp3.OkHttpClient;
  8. import okhttp3.Request;
  9. import okhttp3.ResponseBody;
  10. import org.apache.commons.collections.CollectionUtils;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import java.io.InputStream;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Objects;
  17. public class UploadUtil {
  18. //单文件上传
  19. public static String uploadFile(MultipartFile file) throws Exception {
  20. if (file.isEmpty()) {
  21. throw new RuntimeException("上传文件不能为空");
  22. }
  23. //上传文件
  24. String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(StringPool.DOT));
  25. return Objects.requireNonNull(OssFactory.build()).uploadSuffix(file.getBytes(), suffix);
  26. }
  27. //多文件上传
  28. public static List<String> uploadFiles(List<MultipartFile> files) throws Exception {
  29. if (CollectionUtils.isNotEmpty(files)) {
  30. throw new RuntimeException("上传文件不能为空");
  31. }
  32. List<String> urls = new ArrayList<>();
  33. //上传文件
  34. for (MultipartFile file : files) {
  35. urls.add(uploadFile(file));
  36. }
  37. return urls;
  38. }
  39. public static InputStream download(String path) {
  40. try {
  41. OkHttpClient client = new OkHttpClient();
  42. Request req = new Request.Builder().url(path).build();
  43. InputStream inputStream = null;
  44. okhttp3.Response resp = client.newCall(req).execute();
  45. if (resp.isSuccessful()) {
  46. ResponseBody body = resp.body();
  47. inputStream = body.byteStream();
  48. }
  49. return inputStream;
  50. } catch (Exception e){
  51. throw new RuntimeException("获取文件失败,请检查配置信息", e);
  52. }
  53. }
  54. public static boolean delete(String path) {
  55. return Objects.requireNonNull(OssFactory.build()).delete(path);
  56. }
  57. public static String uploadFileByte(String fileName, byte[] file) throws Exception {
  58. if (file.length == 0) {
  59. throw new RuntimeException("上传文件不能为空");
  60. }
  61. //上传文件
  62. String suffix = Objects.requireNonNull(fileName).substring(fileName.lastIndexOf(StringPool.DOT));
  63. return Objects.requireNonNull(OssFactory.build()).uploadSuffix(file, suffix);
  64. }
  65. /**
  66. * 校验文件
  67. * @param file 上传的文件
  68. * @param rule 校验规则
  69. * @throws MyException 校验失败抛出异常
  70. */
  71. public static void fileTypeValidate(MultipartFile file, FileCheckRuleConfig rule) {
  72. // 2. 文件名校验
  73. String originalFilename = file.getOriginalFilename();
  74. if (StringUtils.isBlank(originalFilename)) {
  75. throw new MyException("文件名不能为空");
  76. }
  77. // 3. 文件扩展名校验
  78. String fileExtension = getFileExtension(originalFilename);
  79. if (!rule.getAllowedExtensions().isEmpty() &&
  80. !rule.getAllowedExtensions().contains(fileExtension.toLowerCase())) {
  81. throw new MyException("不支持的文件类型,仅支持: " +
  82. String.join(", ", rule.getAllowedExtensions()));
  83. }
  84. // 4. MIME类型校验
  85. // String contentType = file.getContentType();
  86. // if (!rule.getAllowedMimeTypes().isEmpty() &&
  87. // !rule.getAllowedMimeTypes().contains(contentType)) {
  88. // throw new MyException("文件类型不匹配,仅支持: " + contentType +
  89. // String.join(", ", rule.getAllowedMimeTypes()));
  90. // }
  91. }
  92. // 安全的获取扩展名方法
  93. private static String getFileExtension(String filename) {
  94. // 处理没有扩展名的情况
  95. int dotIndex = filename.lastIndexOf('.');
  96. if (dotIndex < 0) {
  97. throw new MyException("文件缺少扩展名");
  98. }
  99. return filename.substring(dotIndex + 1).toLowerCase();
  100. }
  101. }