瀏覽代碼

minio 依赖冲突 上传下载

yangyb 1 月之前
父節點
當前提交
cb88da9526

+ 22 - 2
operating-service/pom.xml

@@ -72,10 +72,18 @@
         <dependency>
             <groupId>com.tencentcloudapi</groupId>
             <artifactId>tencentcloud-sdk-java</artifactId>
-            <!-- 请到 https://search.maven.org/search?q=tencentcloud-sdk-java 查询最新版本 -->
             <version>3.1.158</version>
+            <exclusions>
+                <exclusion>
+                    <artifactId>okhttp</artifactId>
+                    <groupId>com.squareup.okhttp</groupId>
+                </exclusion>
+                <exclusion>
+                    <artifactId>logging-interceptor</artifactId>
+                    <groupId>com.squareup.okhttp</groupId>
+                </exclusion>
+            </exclusions>
         </dependency>
-
         <dependency>
             <groupId>com.baidu.aip</groupId>
             <artifactId>java-sdk</artifactId>
@@ -91,7 +99,19 @@
             <groupId>io.minio</groupId>
             <artifactId>minio</artifactId>
             <version>8.2.0</version>
+            <exclusions>
+                <exclusion>
+                    <artifactId>okhttp</artifactId>
+                    <groupId>com.squareup.okhttp3</groupId>
+                </exclusion>
+            </exclusions>
         </dependency>
+        <dependency>
+            <groupId>com.squareup.okhttp3</groupId>
+            <artifactId>okhttp</artifactId>
+            <version>3.14.9</version>
+        </dependency>
+
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>

+ 26 - 8
operating-service/src/main/java/com/txz/operating/config/minio/MinioUtil.java

@@ -19,6 +19,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.io.*;
+import java.net.URL;
 import java.net.URLEncoder;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
@@ -44,9 +45,28 @@ public class MinioUtil {
     /**
      * 上传一个文件
      */
-    public void uploadFile(InputStream stream, String bucket, String objectName) throws Exception {
+    public String uploadFile(InputStream stream, String bucket, String objectName) throws Exception {
         minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
-                .stream(stream, -1, 10485760).build());
+                .stream(stream, -1, 10485760).contentType("Content-Type").build());
+        GetPresignedObjectUrlArgs presignedArgs = GetPresignedObjectUrlArgs.builder()
+                .method(Method.GET)
+                .bucket(bucket)
+                .object(objectName)
+                .expiry(7, TimeUnit.DAYS)
+                .build();
+        minioClient.getPresignedObjectUrl(presignedArgs);
+        String fullUrl = minioClient.getPresignedObjectUrl(presignedArgs);
+        return getPathFromUrl(fullUrl);
+    }
+
+    public String getPathFromUrl(String fullUrl) {
+        try {
+            URL url = new URL(fullUrl);
+            return url.getPath();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
     }
 
     /**
@@ -87,9 +107,7 @@ public class MinioUtil {
      * 下载一个文件
      */
     public InputStream download(String bucket, String objectName) throws Exception {
-        InputStream stream = minioClient.getObject(
-                GetObjectArgs.builder().bucket(bucket).object(objectName).build());
-        return stream;
+        return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
     }
 
     /**
@@ -143,7 +161,7 @@ public class MinioUtil {
      * @Author: MrFugui
      * @Date: 2021/11/15
      */
-    public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) throws Exception {
+    public String getResignedObjectUrl(String bucketName, String objectName, Integer expires) throws Exception {
         GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs
                 .builder().bucket(bucketName).object(objectName).expiry(expires).method(Method.GET).build();
         return minioClient.getPresignedObjectUrl(build);
@@ -240,7 +258,7 @@ public class MinioUtil {
         List<String> uploadUrlList = new ArrayList<>();
         for (int i = 1; i <= chunkCount; i++) {
             reqParams.put("partNumber", String.valueOf(i));
-            String uploadUrl = getPresignedObjectUrl(fileName, reqParams, bucketName);
+            String uploadUrl = getResignedObjectUrl(fileName, reqParams, bucketName);
             uploadUrlList.add(uploadUrl);
         }
 
@@ -280,7 +298,7 @@ public class MinioUtil {
      * @throws XmlParserException        如果解析XML响应时发生错误。
      * @throws InternalException         如果发生内部错误。
      */
-    private String getPresignedObjectUrl(String fileName, Map<String, String> reqParams, String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
+    private String getResignedObjectUrl(String fileName, Map<String, String> reqParams, String bucketName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
         // 使用MinIO客户端生成预签名URL,设置HTTP方法为PUT,并指定文件、存储桶、过期时间及额外查询参数
         return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                 .method(Method.PUT)

+ 30 - 18
operating-service/src/main/java/com/txz/operating/config/minio/FileController.java → operating-service/src/main/java/com/txz/operating/controller/UploadController.java

@@ -1,8 +1,11 @@
-package com.txz.operating.config.minio;
+package com.txz.operating.controller;
 
 
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.IdUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.txz.operating.config.minio.MinioUtil;
+import com.txz.operating.config.minio.SplitFileDto;
 import com.txz.operating.result.Result;
 import io.minio.ObjectWriteResponse;
 import io.swagger.annotations.Api;
@@ -22,7 +25,7 @@ import java.util.List;
 @Api(tags = "文件操作接口")
 @RestController
 @RequestMapping(value = "/file")
-public class FileController {
+public class UploadController {
 
     @Autowired
     MinioUtil minioUtil;
@@ -31,17 +34,21 @@ public class FileController {
 
     @ApiOperation("上传一个文件")
     @PostMapping(value = "/upload")
-    public Result fileUpload(@RequestParam MultipartFile file, @RequestParam String bucket,
-                             @RequestParam(required = false) String objectName) throws Exception {
-        String dir = DateUtil.format(new Date(), NORM_DAY_PATTERN)+ "/";
-//        String fileName = IdUtil.simpleUUID();
+    public Result fileUpload(@RequestParam MultipartFile file) throws Exception {
+        String dir = DateUtil.format(new Date(), NORM_DAY_PATTERN) + "/";
+        String fileName = IdUtil.simpleUUID();
+
+        // 暂时都放在img里边
+        String bucket = "img";
+        String url  = "";
+        String objectName = "";
         minioUtil.createBucket(bucket);
         if (objectName != null) {
-            minioUtil.uploadFile(file.getInputStream(), bucket, objectName + "/" + file.getOriginalFilename());
+            url = minioUtil.uploadFile(file.getInputStream(), bucket, dir + "/" + fileName);
         } else {
-            minioUtil.uploadFile(file.getInputStream(), bucket, file.getOriginalFilename());
+            url = minioUtil.uploadFile(file.getInputStream(), bucket, fileName);
         }
-        return Result.success();
+        return Result.success(url);
     }
 
     @ApiOperation("列出所有的桶")
@@ -61,14 +68,20 @@ public class FileController {
     public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
                              HttpServletResponse response) throws Exception {
         InputStream stream = minioUtil.download(bucket, objectName);
-        ServletOutputStream output = response.getOutputStream();
-        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
-        response.setContentType("application/octet-stream");
-        response.setCharacterEncoding("UTF-8");
-        IOUtils.copy(stream, output);
+        try (ServletOutputStream output = response.getOutputStream()) {
+            // 设置响应头
+            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
+            response.setContentType("application/octet-stream");
+            response.setCharacterEncoding("UTF-8");
+            IOUtils.copy(stream, output);
+            output.flush();
+        } finally {
+            if (stream != null) {
+                stream.close();
+            }
+        }
     }
 
-
     @ApiOperation("删除一个文件")
     @GetMapping(value = "/deleteFile")
     public Result deleteFile(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
@@ -99,9 +112,8 @@ public class FileController {
 
     @GetMapping("/getResignedObjectUrl")
     @ApiOperation("获取一个连接以供下载")
-    public Result getPresignedObjectUrl(@RequestParam String bucket, @RequestParam String objectName, @RequestParam Integer expires) throws Exception {
-
-        return Result.success(minioUtil.getPresignedObjectUrl(bucket, objectName, expires));
+    public Result getResignedObjectUrl(@RequestParam String bucket, @RequestParam String objectName, @RequestParam Integer expires) throws Exception {
+        return Result.success(minioUtil.getResignedObjectUrl(bucket, objectName, expires));
     }
 
     @GetMapping("/listAllFile")