Commit f7196ca48cc0c177852a068a296066456ee6ad75

Authored by liujun001
1 parent 6fcf15c1

对接文件系统

Bsth-admin/src/main/java/com/ruoyi/config/MinioConfig.java
1 1 package com.ruoyi.config;
2 2  
  3 +import io.minio.*;
3 4 import io.minio.errors.*;
  5 +import io.minio.http.Method;
4 6 import org.springframework.beans.factory.InitializingBean;
5 7 import org.springframework.context.annotation.Bean;
6 8 import org.springframework.stereotype.Component;
7 9 import org.springframework.beans.factory.annotation.Value;
8   -import io.minio.MinioClient;
9 10 import org.springframework.web.multipart.MultipartFile;
10 11 import org.springframework.web.util.UriUtils;
11 12  
... ... @@ -15,8 +16,6 @@ import java.nio.charset.StandardCharsets;
15 16 import java.security.InvalidKeyException;
16 17 import java.security.NoSuchAlgorithmException;
17 18  
18   -import io.minio.PutObjectArgs;
19   -
20 19 import javax.annotation.Resource;
21 20  
22 21 @Component
... ... @@ -81,17 +80,43 @@ public class MinioConfig implements InitializingBean {
81 80 */
82 81 public Boolean upload(InputStream inputStream, String fileName, String contentType) {
83 82 // 修饰过的文件名 非源文件名
84   -
  83 +//
  84 +// try {
  85 +// PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucket).object(fileName)
  86 +// .stream(inputStream, inputStream.available(), -1).contentType(contentType).build();
  87 +// //文件名称相同会覆盖
  88 +// minioClient.putObject(objectArgs);
  89 +// } catch (Exception e) {
  90 +// e.printStackTrace();
  91 +// return false;
  92 +// }
85 93 try {
86   - PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucket).object(fileName)
87   - .stream(inputStream, inputStream.available(), -1).contentType(contentType).build();
88   - //文件名称相同会覆盖
89   - minioClient.putObject(objectArgs);
90   - } catch (Exception e) {
91   - e.printStackTrace();
92   - return false;
  94 + ObjectWriteResponse response= minioClient.putObject(PutObjectArgs.builder()
  95 + .bucket(bucket) // 替换为你实际的存储桶名称
  96 + .object(fileName)
  97 + .stream(inputStream, inputStream.available(), -1)
  98 + .contentType(contentType)
  99 + .build());
  100 + return true;
  101 + } catch (ErrorResponseException e) {
  102 + throw new RuntimeException(e);
  103 + } catch (InsufficientDataException e) {
  104 + throw new RuntimeException(e);
  105 + } catch (InternalException e) {
  106 + throw new RuntimeException(e);
  107 + } catch (InvalidKeyException e) {
  108 + throw new RuntimeException(e);
  109 + } catch (InvalidResponseException e) {
  110 + throw new RuntimeException(e);
  111 + } catch (IOException e) {
  112 + throw new RuntimeException(e);
  113 + } catch (NoSuchAlgorithmException e) {
  114 + throw new RuntimeException(e);
  115 + } catch (ServerException e) {
  116 + throw new RuntimeException(e);
  117 + } catch (XmlParserException e) {
  118 + throw new RuntimeException(e);
93 119 }
94   - return true;
95 120 }
96 121  
97 122  
... ...
Bsth-admin/src/main/java/com/ruoyi/service/impl/sign/in/resource/LinggangSignInResourceServiceImpl.java
... ... @@ -135,13 +135,13 @@ public class LinggangSignInResourceServiceImpl extends ServiceImpl<LinggangSignI
135 135 entities = entities.stream().map(en -> {
136 136 StringBuilder builder = new StringBuilder();
137 137 builder.append("sign");
138   - builder.append(File.separator);
  138 + builder.append("/");
139 139 builder.append("images");
140   - builder.append(File.separator);
  140 + builder.append("/");
141 141 builder.append(en.getDevice());
142   - builder.append(File.separator);
  142 + builder.append("/");
143 143 builder.append(en.getSignId());
144   - builder.append(File.separator);
  144 + builder.append("/");
145 145 builder.append(UUID.randomUUID().toString().replace("-", ""));
146 146 builder.append(".");
147 147 builder.append(en.getPicType());
... ...
Bsth-admin/src/main/java/com/ruoyi/utils/UploadUtil.java
... ... @@ -5,6 +5,7 @@ import com.ruoyi.common.core.domain.ResponseResult;
5 5 import com.ruoyi.common.utils.StringUtils;
6 6 import com.ruoyi.common.utils.file.FileUploadUtils;
7 7 import com.ruoyi.config.BsthSystemConfig;
  8 +import com.ruoyi.config.MinioConfig;
8 9 import lombok.extern.slf4j.Slf4j;
9 10 import org.apache.commons.io.FileUtils;
10 11 import org.apache.commons.io.IOUtils;
... ... @@ -28,6 +29,8 @@ public class UploadUtil {
28 29  
29 30 @Autowired
30 31 private BsthSystemConfig bsthSystemConfig;
  32 + @Autowired
  33 + private MinioConfig minioConfig;
31 34  
32 35 /**
33 36 * 上传图片接口
... ... @@ -50,23 +53,8 @@ public class UploadUtil {
50 53 content = FileUploadUtils.choosePrictureContent(content);
51 54 byte[] bytes = Base64.decode(content);
52 55 inputStream = new ByteArrayInputStream(bytes);
53   - bufferedImage = ImageIO.read(inputStream);
54   - if (bufferedImage.getWidth() < 0 || bufferedImage.getHeight() < 0) {
55   - return ResponseResult.error("请上传正确的图片");
56   - }
57   -
58   - StringBuilder filePath = new StringBuilder();
59   - filePath.append(bsthSystemConfig.getImageBasePath());
60   - filePath.append(File.separator);
61   - filePath.append(path);
62   -
63   - File file = new File(filePath.toString());
64   - if(!file.exists() || !file.isDirectory()){
65   - FileUtils.forceMkdirParent(file);
66   - }
67 56  
68   - ImageIO.write(bufferedImage, type, file);
69   - bufferedImage.flush();
  57 + minioConfig.upload(inputStream,path,"application/png");
70 58  
71 59 return ResponseResult.success(Boolean.TRUE);
72 60 } catch (Exception e) {
... ...