nahida 2 сар өмнө
parent
commit
c025aa6064

+ 32 - 6
src/main/java/com/zksy/service/MinioFileStorageService.java

@@ -1,16 +1,10 @@
 package com.zksy.service;
 
-import io.minio.errors.*;
-import org.springframework.stereotype.Component;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
-import java.io.IOException;
 import java.io.InputStream;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.util.ArrayList;
 import java.util.List;
 
 
@@ -28,6 +22,38 @@ public interface MinioFileStorageService {
      */
     public void downLoadFile(String filePath, HttpServletResponse response);
 
+    /**
+     * 获取文件流
+     * @param filePath 文件地址
+     * @return 文件输入流
+     * @throws Exception
+     */
+    public InputStream getFileStream(String filePath) throws Exception;
+
+    /**
+     * 获取文件外链(预签名URL)
+     * @param filePath 文件地址
+     * @param expiry 过期时间(秒),最大通常为7天(604800秒)
+     * @return 文件访问URL
+     * @throws Exception
+     */
+    public String getFileUrl(String filePath, Integer expiry) throws Exception;
+
+    /**
+     * 判断文件是否存在
+     * @param filePath 文件地址
+     * @return true表示存在,false表示不存在
+     */
+    public boolean fileExists(String filePath);
+
+    /**
+     * 获取文件的字节数组
+     * @param filePath 文件地址
+     * @return 文件的字节数组
+     * @throws Exception
+     */
+    public byte[] getFileBytes(String filePath) throws Exception;
+
     /**
      * 上传文件
      * @param file 文件

+ 65 - 9
src/main/java/com/zksy/service/impl/MinioFileStorageServiceImpl.java

@@ -10,13 +10,9 @@ import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.PostConstruct;
-import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
-import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
@@ -57,11 +53,7 @@ public class MinioFileStorageServiceImpl implements MinioFileStorageService {
     @Override
     public void downLoadFile(String filePath, HttpServletResponse response) {
         try {
-            InputStream inputStream = minioClient.getObject(GetObjectArgs.builder()
-                    .bucket(minIOConfigProperties.getBucket())
-                    .object(filePath)
-                    .build()
-            );
+            InputStream inputStream = getFileStream(filePath);
             response.setContentType("application/octet-stream");
             response.setHeader("Content-Disposition", "attachment; filename=" + filePath);
             OutputStream out = response.getOutputStream();
@@ -72,12 +64,76 @@ public class MinioFileStorageServiceImpl implements MinioFileStorageService {
             }
             out.flush();
             out.close();
+            inputStream.close();
         } catch (Exception e) {
             log.error("下载文件失败", e);
             throw new RuntimeException("下载文件失败");
         }
     }
 
+    @Override
+    public InputStream getFileStream(String filePath) throws Exception {
+        try {
+            return minioClient.getObject(GetObjectArgs.builder()
+                    .bucket(minIOConfigProperties.getBucket())
+                    .object(filePath)
+                    .build()
+            );
+        } catch (Exception e) {
+            log.error("获取文件流失败", e);
+            throw new Exception("获取文件流失败", e);
+        }
+    }
+
+    @Override
+    public String getFileUrl(String filePath, Integer expiry) throws Exception {
+        try {
+            return minioClient.getPresignedObjectUrl(
+                    GetPresignedObjectUrlArgs.builder()
+                            .method(io.minio.http.Method.GET)
+                            .bucket(minIOConfigProperties.getBucket())
+                            .object(filePath)
+                            .expiry(expiry == null ? 60 * 60 * 24 : expiry) // 默认一天
+                            .build()
+            );
+        } catch (Exception e) {
+            log.error("获取文件访问URL失败", e);
+            throw new Exception("获取文件访问URL失败", e);
+        }
+    }
+
+    @Override
+    public boolean fileExists(String filePath) {
+        try {
+            minioClient.statObject(
+                    StatObjectArgs.builder()
+                            .bucket(minIOConfigProperties.getBucket())
+                            .object(filePath)
+                            .build()
+            );
+            return true;
+        } catch (Exception e) {
+            // 如果文件不存在,通常会抛出异常
+            return false;
+        }
+    }
+
+    @Override
+    public byte[] getFileBytes(String filePath) throws Exception {
+        try (InputStream inputStream = getFileStream(filePath);
+             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
+            byte[] buffer = new byte[1024];
+            int length;
+            while ((length = inputStream.read(buffer)) > 0) {
+                outputStream.write(buffer, 0, length);
+            }
+            return outputStream.toByteArray();
+        } catch (Exception e) {
+            log.error("获取文件字节数组失败", e);
+            throw new Exception("获取文件字节数组失败", e);
+        }
+    }
+
     /**
      *
      * @param file 文件