diff --git a/pom.xml b/pom.xml
index e4a0e65e..1d2924c8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -355,6 +355,13 @@
Java-WebSocket
1.5.4
+
+
+
+ io.minio
+ minio
+ 3.0.10
+
diff --git a/src/main/java/com/glxp/api/config/MinioConfig.java b/src/main/java/com/glxp/api/config/MinioConfig.java
new file mode 100644
index 00000000..39f14056
--- /dev/null
+++ b/src/main/java/com/glxp/api/config/MinioConfig.java
@@ -0,0 +1,51 @@
+package com.glxp.api.config;
+
+import io.minio.MinioClient;
+import io.minio.errors.InvalidEndpointException;
+import io.minio.errors.InvalidPortException;
+import lombok.Data;
+import lombok.RequiredArgsConstructor;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@RequiredArgsConstructor
+public class MinioConfig {
+
+ @Data
+ @ConfigurationProperties("minio")
+ @Configuration
+ public class MinioProperties {
+ /**
+ * 服务地址
+ */
+ private String endpoint;
+
+
+ /**
+ * 用户名
+ */
+ private String accessKey;
+
+ /**
+ * 密码
+ */
+ private String secretKey;
+ }
+
+
+ /**
+ * 初始化客户端
+ *
+ * @return 客户端
+ */
+ @Bean
+ public MinioClient minioClient(MinioProperties minioProperties) {
+ try {
+ return new MinioClient(minioProperties.getEndpoint(), minioProperties.getAccessKey(), minioProperties.getSecretKey());
+ } catch (InvalidEndpointException | InvalidPortException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/src/main/java/com/glxp/api/upload/uploadController.java b/src/main/java/com/glxp/api/upload/uploadController.java
index 861b61ea..e8bc5afe 100644
--- a/src/main/java/com/glxp/api/upload/uploadController.java
+++ b/src/main/java/com/glxp/api/upload/uploadController.java
@@ -3,6 +3,7 @@ package com.glxp.api.upload;
import com.glxp.api.common.enums.ResultEnum;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
+import com.glxp.api.util.MinioUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
@@ -25,6 +26,10 @@ public class uploadController {
@Value("${file_path}")
private String filePath;
+ @Value("${minio_path}")
+ private String minioPath;
+ @Value("${minio_url}")
+ private String minioUrl;
/**
* 上传PDF模板
@@ -116,21 +121,23 @@ public class uploadController {
return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件只能是 jpg,png,doc,pdf 格式");
}
String newName = UUID.randomUUID() + fileType;//生成新文件名
- String savePath = filePath + "/register/file/" + type;
+ String savePath = minioPath + "/register/file/" + type;
+ String fileFullName = savePath + "/" + newName;
- File file1 = new File(savePath);
- if (!file1.exists()) {// 判断目录是否存在
- file1.mkdirs();// 创建多层目录
- }
- file1 = new File(savePath + "/" + newName);
+// File file1 = new File(savePath);
+// if (!file1.exists()) {// 判断目录是否存在
+// file1.mkdirs();// 创建多层目录
+// }
+// file1 = new File(savePath + "/" + newName);
try {
- file.transferTo(file1);
+// file.transferTo(file1);
+ MinioUtil.uploadFile(fileFullName, file);
Map rMap = new HashMap<>();
rMap.put("msg", "上传成功");
- rMap.put("name", newName);
+ rMap.put("name", minioUrl + fileFullName);
rMap.put("type", type);
return ResultVOUtils.success(rMap);
- } catch (IOException e) {
+ } catch (Exception e) {
e.printStackTrace();
}
return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传失败");
diff --git a/src/main/java/com/glxp/api/util/MinioUtil.java b/src/main/java/com/glxp/api/util/MinioUtil.java
new file mode 100644
index 00000000..4b82148a
--- /dev/null
+++ b/src/main/java/com/glxp/api/util/MinioUtil.java
@@ -0,0 +1,156 @@
+package com.glxp.api.util;
+
+import cn.hutool.core.io.FileUtil;
+import io.minio.MinioClient;
+import io.minio.errors.*;
+import io.minio.http.Method;
+import io.minio.policy.PolicyType;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+import org.xmlpull.v1.XmlPullParserException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * minio工具类
+ */
+@Slf4j
+@Component
+public class MinioUtil {
+
+ private static MinioClient minioClient;
+
+ @Autowired
+ public void setMinioClient(MinioClient minioClient) {
+ MinioUtil.minioClient = minioClient;
+ }
+
+ /**
+ * 如不存在则创建存储bucket
+ *
+ * @return Boolean
+ */
+ private static void makeBucket(String bucketName) {
+ try {
+
+ boolean exists = minioClient.bucketExists(bucketName);
+ if (!exists) {
+ minioClient.makeBucket(bucketName);
+ minioClient.setBucketPolicy(bucketName, "*", PolicyType.READ_ONLY);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * 上传文件
+ *
+ * @param fileFullName 文件全地址
+ * @param file 文件
+ */
+ public static void uploadFile(String fileFullName, MultipartFile file) {
+ try {
+ //从fileName中获取bucketName和objectName
+ String reg = "/(\\S*?)(/\\S*)";
+ String bucketName = fileFullName.replaceFirst(reg, "$1");
+ String objectName = fileFullName.replaceFirst(reg, "$2");
+ makeBucket(bucketName);
+ minioClient.putObject(bucketName, objectName, file.getInputStream(), FileUtil.getMimeType(fileFullName));
+ } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
+ InvalidKeyException | ErrorResponseException | InternalException | InvalidArgumentException |
+ XmlPullParserException | NoResponseException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * 上传文件 内置关闭流
+ *
+ * @param fileFullName 文件全地址
+ * @param file 文件
+ * @param fileContentType 文件contentType
+ * @throws Exception
+ */
+ public static void uploadFile(String fileFullName, InputStream file, String fileContentType) throws InvalidArgumentException, XmlPullParserException, NoResponseException {
+ try {
+ //从fileName中获取bucketName和objectName
+ String reg = "/(\\S*?)(/\\S*)";
+ String bucketName = fileFullName.replaceFirst(reg, "$1");
+ String objectName = fileFullName.replaceFirst(reg, "$2");
+ makeBucket(bucketName);
+ minioClient.putObject(bucketName, objectName, file, fileContentType);
+ } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
+ InvalidKeyException | ErrorResponseException |
+ InternalException e) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ } finally {
+ try {
+ file.close();
+ } catch (IOException e) {
+ log.error(e.getMessage(), e);
+ }
+ }
+ }
+
+ /**
+ * 获取文件地址带有效期
+ *
+ * @param fileFullName 文件全名称带路径
+ * @param expires 过期时间
+ * @return 完整访问地址
+ */
+ public static String getPresignedObjectUrl(String fileFullName, Integer expires) {
+ String reg = "/(\\S*?)(/\\S*)";
+ String bucketName = fileFullName.replaceFirst(reg, "$1");
+ String objectName = fileFullName.replaceFirst(reg, "$2");
+ try {
+ return minioClient.getPresignedObjectUrl(Method.GET, bucketName, objectName, expires, null);
+ } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
+ InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
+ InternalException | InvalidExpiresRangeException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ /**
+ * 获取文件地址带有效期
+ *
+ * @param fileFullName 文件全名称带路径
+ * @return inputStream
+ */
+ public static InputStream getFileInputStream(String fileFullName) {
+ String reg = "/(\\S*?)(/\\S*)";
+ String bucketName = fileFullName.replaceFirst(reg, "$1");
+ String objectName = fileFullName.replaceFirst(reg, "$2");
+ try {
+ return minioClient.getObject(bucketName, objectName);
+ } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
+ InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
+ InternalException | InvalidArgumentException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static void deleteFile(String fileFullName) {
+ String reg = "/(\\S*?)(/\\S*)";
+ String bucketName = fileFullName.replaceFirst(reg, "$1");
+ String objectName = fileFullName.replaceFirst(reg, "$2");
+ try {
+ minioClient.removeObject(bucketName, objectName);
+ } catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
+ InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
+ InternalException e) {
+// throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index bd845221..ef8e6776 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -3,9 +3,9 @@ server:
spring:
datasource:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
- jdbc-url: jdbc:p6spy:mysql://127.0.0.1:3306/udi_spms_pt?allowMultiQueries=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
+ jdbc-url: jdbc:p6spy:mysql://192.168.0.66:3364/udi_spms_px?allowMultiQueries=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
- password: 123456
+ password: Glxp@6066
hikari:
connection-timeout: 60000
maximum-pool-size: 60
@@ -37,6 +37,10 @@ ok:
write-timeout: 3000
max-idle-connections: 200
keep-alive-duration: 300
+minio:
+ endpoint: http://139.9.219.60:9000
+ access-key: minioadmin
+ secret-key: minioadmin
logging:
@@ -44,8 +48,10 @@ logging:
com.glxp.api.dao: debug
file_path: d:/udi/udiwms/udiwmsfile/
-back_file_path: d:/share/udisps/back/
+minio_path: /udi
+minio_url: http://139.9.219.60:9000
err_path: d:/udi/udiwms/err/
+back_file_path: d:/share/udisps/back/
UDI_KEY: 6b137c66-6286-46c6-8efa-c2f5dd9237df
UDI_SERVER_URL: https://www.udims.com/UDI_DL_Server_test
SPMS_KEY: lCOdWCBKS6Kw45wdnnqUTELXyuSKnXEs