|
@@ -10,13 +10,9 @@ import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
|
-import javax.servlet.ServletOutputStream;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
|
-import java.security.InvalidKeyException;
|
|
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
|
@@ -57,11 +53,7 @@ public class MinioFileStorageServiceImpl implements MinioFileStorageService {
|
|
|
@Override
|
|
@Override
|
|
|
public void downLoadFile(String filePath, HttpServletResponse response) {
|
|
public void downLoadFile(String filePath, HttpServletResponse response) {
|
|
|
try {
|
|
try {
|
|
|
- InputStream inputStream = minioClient.getObject(GetObjectArgs.builder()
|
|
|
|
|
- .bucket(minIOConfigProperties.getBucket())
|
|
|
|
|
- .object(filePath)
|
|
|
|
|
- .build()
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ InputStream inputStream = getFileStream(filePath);
|
|
|
response.setContentType("application/octet-stream");
|
|
response.setContentType("application/octet-stream");
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + filePath);
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + filePath);
|
|
|
OutputStream out = response.getOutputStream();
|
|
OutputStream out = response.getOutputStream();
|
|
@@ -72,12 +64,76 @@ public class MinioFileStorageServiceImpl implements MinioFileStorageService {
|
|
|
}
|
|
}
|
|
|
out.flush();
|
|
out.flush();
|
|
|
out.close();
|
|
out.close();
|
|
|
|
|
+ inputStream.close();
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("下载文件失败", e);
|
|
log.error("下载文件失败", e);
|
|
|
throw new RuntimeException("下载文件失败");
|
|
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 文件
|
|
* @param file 文件
|