From 3a46ad74a295bda71e54eca0500bc430a601664d Mon Sep 17 00:00:00 2001 From: "JinLu.Yin" <411641505@qq.com> Date: Wed, 27 Jul 2022 20:48:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/CommonController.java | 16 ++++ ktg-common/pom.xml | 6 ++ .../com/ktg/common/config/MinioConfig.java | 80 +++++++++++++++++++ .../com/ktg/common/utils/ServletUtils.java | 42 ++++++++++ .../common/utils/file/FileUploadUtils.java | 73 +++++++++++++++++ .../com/ktg/common/utils/file/MinioUtil.java | 38 +++++++++ 6 files changed, 255 insertions(+) create mode 100644 ktg-common/src/main/java/com/ktg/common/config/MinioConfig.java create mode 100644 ktg-common/src/main/java/com/ktg/common/utils/file/MinioUtil.java diff --git a/ktg-admin/src/main/java/com/ktg/web/controller/common/CommonController.java b/ktg-admin/src/main/java/com/ktg/web/controller/common/CommonController.java index a177cc5..dffb970 100644 --- a/ktg-admin/src/main/java/com/ktg/web/controller/common/CommonController.java +++ b/ktg-admin/src/main/java/com/ktg/web/controller/common/CommonController.java @@ -161,4 +161,20 @@ public class CommonController log.error("下载文件失败", e); } } + + @PostMapping("/uploadMinio") + public AjaxResult uploadFileMinio(MultipartFile file) throws Exception{ + try{ + String fileName = FileUploadUtils.uploadMinio(file); + AjaxResult rt = AjaxResult.success(); + rt.put("url",fileName); + rt.put("fileName",fileName); + rt.put("newFileName",FileUtils.getName(fileName)); + rt.put("originalFileName",file.getOriginalFilename()); + return rt; + }catch (Exception e){ + return AjaxResult.error(e.getMessage()); + } + } + } diff --git a/ktg-common/pom.xml b/ktg-common/pom.xml index 2acd018..ed6212b 100644 --- a/ktg-common/pom.xml +++ b/ktg-common/pom.xml @@ -131,6 +131,12 @@ 5.8.0.M3 + + io.minio + minio + 8.2.1 + + \ No newline at end of file diff --git a/ktg-common/src/main/java/com/ktg/common/config/MinioConfig.java b/ktg-common/src/main/java/com/ktg/common/config/MinioConfig.java new file mode 100644 index 0000000..8bd046b --- /dev/null +++ b/ktg-common/src/main/java/com/ktg/common/config/MinioConfig.java @@ -0,0 +1,80 @@ +package com.ktg.common.config; + +import io.minio.MinioClient; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Minio 配置信息 + * + */ +@Configuration +@ConfigurationProperties(prefix = "minio") +public class MinioConfig { + /** + * 服务地址 + */ + private static String url; + + /** + * 用户名 + */ + private static String accessKey; + + /** + * 密码 + */ + private static String secretKey; + + /** + * 存储桶名称 + */ + private static String bucketName; + + public static String getUrl() + { + return url; + } + + public void setUrl(String url) + { + MinioConfig.url = url; + } + + public static String getAccessKey() + { + return accessKey; + } + + public void setAccessKey(String accessKey) + { + MinioConfig.accessKey = accessKey; + } + + public static String getSecretKey() + { + return secretKey; + } + + public void setSecretKey(String secretKey) + { + MinioConfig.secretKey = secretKey; + } + + public static String getBucketName() + { + return bucketName; + } + + public void setBucketName(String bucketName) + { + MinioConfig.bucketName = bucketName; + } + + @Bean + public MinioClient getMinioClient() + { + return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build(); + } +} diff --git a/ktg-common/src/main/java/com/ktg/common/utils/ServletUtils.java b/ktg-common/src/main/java/com/ktg/common/utils/ServletUtils.java index 2cd5b04..30d6ea6 100644 --- a/ktg-common/src/main/java/com/ktg/common/utils/ServletUtils.java +++ b/ktg-common/src/main/java/com/ktg/common/utils/ServletUtils.java @@ -1,9 +1,14 @@ package com.ktg.common.utils; import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.net.URLDecoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; + +import com.ktg.common.constant.Constants; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -143,4 +148,41 @@ public class ServletUtils String ajax = request.getParameter("__ajax"); return StringUtils.inStringIgnoreCase(ajax, "json", "xml"); } + + /** + * 内容编码 + * + * @param str 内容 + * @return 编码后的内容 + */ + public static String urlEncode(String str) + { + try + { + return URLEncoder.encode(str, Constants.UTF8); + } + catch (UnsupportedEncodingException e) + { + return StringUtils.EMPTY; + } + } + + /** + * 内容解码 + * + * @param str 内容 + * @return 解码后的内容 + */ + public static String urlDecode(String str) + { + try + { + return URLDecoder.decode(str, Constants.UTF8); + } + catch (UnsupportedEncodingException e) + { + return StringUtils.EMPTY; + } + } + } diff --git a/ktg-common/src/main/java/com/ktg/common/utils/file/FileUploadUtils.java b/ktg-common/src/main/java/com/ktg/common/utils/file/FileUploadUtils.java index a49b754..f4bcbd0 100644 --- a/ktg-common/src/main/java/com/ktg/common/utils/file/FileUploadUtils.java +++ b/ktg-common/src/main/java/com/ktg/common/utils/file/FileUploadUtils.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.nio.file.Paths; import java.util.Objects; +import com.ktg.common.config.MinioConfig; import com.ktg.common.config.RuoYiConfig; import org.apache.commons.io.FilenameUtils; import org.springframework.web.multipart.MultipartFile; @@ -48,6 +49,17 @@ public class FileUploadUtils return defaultBaseDir; } + + /** + * Minio默认上传的地址 + */ + private static String bucketName = MinioConfig.getBucketName(); + + public static String getBucketName() + { + return bucketName; + } + /** * 以默认配置进行文件上传 * @@ -118,6 +130,67 @@ public class FileUploadUtils return getPathFileName(baseDir, fileName); } + /** + * 以默认BucketName配置上传到Minio服务器 + * + * @param file 上传的文件 + * @return 文件名称 + * @throws Exception + */ + public static final String uploadMinio(MultipartFile file) throws IOException + { + try + { + return uploadMinino(getBucketName(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); + } + catch (Exception e) + { + throw new IOException(e.getMessage(), e); + } + } + + /** + * 自定义bucketName配置上传到Minio服务器 + * + * @param file 上传的文件 + * @return 文件名称 + * @throws Exception + */ + public static final String uploadMinio(MultipartFile file, String bucketName) throws IOException + { + try + { + return uploadMinino(bucketName, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION); + } + catch (Exception e) + { + throw new IOException(e.getMessage(), e); + } + } + + private static final String uploadMinino(String bucketName, MultipartFile file, String[] allowedExtension) + throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, + InvalidExtensionException + { + int fileNamelength = file.getOriginalFilename().length(); + if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) + { + throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); + } + assertAllowed(file, allowedExtension); + try + { + String fileName = extractFilename(file); + String pathFileName = MinioUtil.uploadFile(bucketName, fileName, file); + return pathFileName; + } + catch (Exception e) + { + throw new IOException(e.getMessage(), e); + } + } + + /** * 编码文件名 */ diff --git a/ktg-common/src/main/java/com/ktg/common/utils/file/MinioUtil.java b/ktg-common/src/main/java/com/ktg/common/utils/file/MinioUtil.java new file mode 100644 index 0000000..e537a9f --- /dev/null +++ b/ktg-common/src/main/java/com/ktg/common/utils/file/MinioUtil.java @@ -0,0 +1,38 @@ +package com.ktg.common.utils.file; + +import com.ktg.common.utils.ServletUtils; +import com.ktg.common.utils.spring.SpringUtils; +import io.minio.GetPresignedObjectUrlArgs; +import io.minio.MinioClient; +import io.minio.PutObjectArgs; +import io.minio.http.Method; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.io.InputStream; + +public class MinioUtil { + /** + * 上传文件 + * + * @param bucketName 桶名称 + * @param fileName + * @throws IOException + */ + public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException + { + String url = ""; + MinioClient minioClient = SpringUtils.getBean(MinioClient.class); + try (InputStream inputStream = multipartFile.getInputStream()) + { + minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build()); + url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build()); + url = url.substring(0, url.indexOf('?')); + return ServletUtils.urlDecode(url); + } + catch (Exception e) + { + throw new IOException(e.getMessage(), e); + } + } +}