From 8a7b666f0bed7c1dc03767166c7ecd602552a5c8 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Mon, 18 May 2026 15:24:46 +0800
Subject: [PATCH] 优化
---
ard-modules/ard-modules-file/src/main/java/com/ard/file/service/MinioSysFileServiceImpl.java | 90 ++++++++++++++++++++++++++++++++++----------
1 files changed, 69 insertions(+), 21 deletions(-)
diff --git a/ard-modules/ard-modules-file/src/main/java/com/ard/file/service/MinioSysFileServiceImpl.java b/ard-modules/ard-modules-file/src/main/java/com/ard/file/service/MinioSysFileServiceImpl.java
index 9d291d1..c151313 100644
--- a/ard-modules/ard-modules-file/src/main/java/com/ard/file/service/MinioSysFileServiceImpl.java
+++ b/ard-modules/ard-modules-file/src/main/java/com/ard/file/service/MinioSysFileServiceImpl.java
@@ -1,6 +1,9 @@
package com.ard.file.service;
import java.io.InputStream;
+
+import io.minio.GetPresignedObjectUrlArgs;
+import io.minio.http.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
@@ -20,8 +23,7 @@
*/
@Primary
@Service
-public class MinioSysFileServiceImpl implements ISysFileService
-{
+public class MinioSysFileServiceImpl implements ISysFileService {
@Autowired
private MinioConfig minioConfig;
@@ -36,11 +38,9 @@
* @throws Exception
*/
@Override
- public String uploadFile(MultipartFile file) throws Exception
- {
+ public String uploadFile(MultipartFile file) throws Exception {
InputStream inputStream = null;
- try
- {
+ try {
String fileName = FileUploadUtils.extractFilename(file);
inputStream = file.getInputStream();
PutObjectArgs args = PutObjectArgs.builder()
@@ -51,33 +51,81 @@
.build();
client.putObject(args);
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
throw new RuntimeException("Minio Failed to upload file", e);
- }
- finally
- {
+ } finally {
IoUtils.closeQuietly(inputStream);
}
}
/**
+ * Minio文件上传接口
+ *
+ * @param file 上传的文件
+ * @param bucketName 桶名称
+ * @return 访问地址
+ * @author 刘苏义
+ * @description 指定桶名称上传文件
+ * @date 2024/8/5 11:37
+ */
+ @Override
+ public String uploadFile(MultipartFile file, String bucketName) throws Exception {
+ String fileName = FileUploadUtils.extractFilename(file);
+ InputStream inputStream = file.getInputStream();
+ PutObjectArgs args = PutObjectArgs.builder()
+ .bucket(bucketName)
+ .object(fileName)
+ .stream(inputStream, file.getSize(), -1)
+ .contentType(file.getContentType())
+ .build();
+ client.putObject(args);
+ IoUtils.closeQuietly(inputStream);
+ return minioConfig.getUrl() + "/" + bucketName + "/" + fileName;
+ }
+
+ @Override
+ public String uploadFile(MultipartFile file, String bucketName, String path) throws Exception {
+ //判断文件是否为空
+ if (null == file || 0 == file.getSize()) {
+ return "";
+ }
+ //文件名
+ String fileName = path + "/" + FileUploadUtils.extractFilename(file);
+ InputStream inputStream = file.getInputStream();
+ /*上传对象*/
+ PutObjectArgs putObjectArgs = PutObjectArgs
+ .builder()
+ .bucket(bucketName)
+ .object(fileName)
+ .stream(inputStream, file.getSize(), -1)
+ .contentType(file.getContentType())
+ .build();
+ client.putObject(putObjectArgs);
+ inputStream.close();
+ /*获取url*/
+ GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs
+ .builder()
+ .bucket(bucketName)
+ .object(fileName)
+ .method(Method.GET)
+ .build();
+ String presignedObjectUrl = client.getPresignedObjectUrl(getPresignedObjectUrlArgs);
+ String ObjectUrl = presignedObjectUrl.substring(0, presignedObjectUrl.indexOf("?"));
+ return ObjectUrl;
+ }
+
+ /**
* Minio文件删除接口
- *
+ *
* @param fileUrl 文件访问URL
* @throws Exception
*/
@Override
- public void deleteFile(String fileUrl) throws Exception
- {
- try
- {
- String minioFile = StringUtils.substringAfter(fileUrl, minioConfig.getBucketName()+ "/");
+ public void deleteFile(String fileUrl) throws Exception {
+ try {
+ String minioFile = StringUtils.substringAfter(fileUrl, minioConfig.getBucketName() + "/");
client.removeObject(RemoveObjectArgs.builder().bucket(minioConfig.getBucketName()).object(minioFile).build());
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
throw new RuntimeException("Minio Failed to delete file", e);
}
}
--
Gitblit v1.9.3