NewFileService.java 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.sqx.modules.file.service;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.aliyun.oss.OSS;
  4. import com.aliyun.oss.OSSClientBuilder;
  5. import com.sqx.common.exception.SqxException;
  6. import com.sqx.config.MinioConfig;
  7. import com.sqx.modules.common.service.CommonInfoService;
  8. import com.sqx.modules.file.utils.FileUploadUtils;
  9. import com.sqx.modules.file.utils.MimeTypeUtils;
  10. import io.minio.MinioClient;
  11. import io.minio.PutObjectArgs;
  12. import lombok.RequiredArgsConstructor;
  13. import org.apache.commons.lang3.time.DateFormatUtils;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.Date;
  20. import java.util.Objects;
  21. import java.util.UUID;
  22. /**
  23. * @author codingliang
  24. * @date 2025-10-21
  25. */
  26. @Service
  27. @RequiredArgsConstructor
  28. public class NewFileService {
  29. private final CommonInfoService commonInfoService;
  30. private final MinioClient minioClient;
  31. private final MinioConfig minioConfig;
  32. public String upload(MultipartFile file) {
  33. // 新增图片类型验证
  34. String extension = FileUploadUtils.getExtension(file);
  35. if (!FileUploadUtils.isAllowedExtension(extension, MimeTypeUtils.IMAGE_EXTENSION)) {
  36. throw new SqxException("文件必须为图片类型");
  37. }
  38. // 上传方式 1阿里云oss 2本地 3 minio
  39. String value = commonInfoService.findOne(234).getValue();
  40. if(StrUtil.equals(value, "1")){
  41. // 创建OSSClient实例。
  42. OSS ossClient = new OSSClientBuilder().build(commonInfoService.findOne(68).getValue(), commonInfoService.findOne(69).getValue(), commonInfoService.findOne(70).getValue());
  43. String suffix = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf("."));
  44. // 上传文件流
  45. InputStream inputStream;
  46. try {
  47. inputStream = new ByteArrayInputStream(file.getBytes());
  48. } catch (IOException e) {
  49. throw new SqxException("文件上传失败!" + e.getMessage());
  50. }
  51. String completePath = getPath(suffix);
  52. ossClient.putObject(commonInfoService.findOne(71).getValue(), completePath, inputStream);
  53. ossClient.shutdown();
  54. return commonInfoService.findOne(72).getValue()+ "/" + completePath;
  55. } else if (StrUtil.equals(value, "3")) {
  56. String suffix = file.getOriginalFilename().substring(Objects.requireNonNull(file.getOriginalFilename()).lastIndexOf("."));
  57. String completePath = getPath(suffix);
  58. try {
  59. minioClient.putObject(PutObjectArgs.builder()
  60. .bucket(minioConfig.getBucket())
  61. .object(completePath)
  62. .stream(file.getInputStream(), file.getSize(), -1)
  63. .build());
  64. return commonInfoService.findOne(72).getValue() + "/" + completePath;
  65. } catch (Exception e) {
  66. throw new SqxException("文件上传失败!");
  67. }
  68. } else{
  69. try {
  70. String http = commonInfoService.findOne(19).getValue();
  71. String[] split = http.split("://");
  72. // 上传文件路径
  73. String filePath ="/mydata/project/media/" + split[1] + "/file/uploadPath";
  74. // 上传并返回新文件名称
  75. String fileName = FileUploadUtils.upload(filePath, file);
  76. return http + fileName;
  77. } catch (Exception e) {
  78. throw new SqxException("文件上传失败!");
  79. }
  80. }
  81. }
  82. private String getPath(String suffix) {
  83. // 生成uuid
  84. String uuid = UUID.randomUUID().toString().replaceAll("-", "");
  85. // 文件路径
  86. String path = DateFormatUtils.format(new Date(), "yyyyMMdd") + "/" + uuid;
  87. return path + suffix;
  88. }
  89. }