|
@@ -1,6 +1,5 @@
|
|
|
package com.energy.manage.service.util;
|
|
package com.energy.manage.service.util;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
import com.energy.manage.service.domain.dto.minio.ObjectItem;
|
|
import com.energy.manage.service.domain.dto.minio.ObjectItem;
|
|
|
import com.xxl.job.core.util.IpUtil;
|
|
import com.xxl.job.core.util.IpUtil;
|
|
|
import io.minio.*;
|
|
import io.minio.*;
|
|
@@ -108,10 +107,21 @@ public class MinioUtils {
|
|
|
List<String> names = new ArrayList<>(multipartFile.length);
|
|
List<String> names = new ArrayList<>(multipartFile.length);
|
|
|
for (MultipartFile file : multipartFile) {
|
|
for (MultipartFile file : multipartFile) {
|
|
|
String fileName = file.getOriginalFilename();
|
|
String fileName = file.getOriginalFilename();
|
|
|
- String[] split = fileName.split("\\.");
|
|
|
|
|
- if (split.length > 1) {
|
|
|
|
|
- fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
|
|
|
|
|
|
|
+ // String[] split = fileName.split("\\.");
|
|
|
|
|
+ // if (split.length > 1) {
|
|
|
|
|
+ // fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
|
|
|
|
|
+ // } else {
|
|
|
|
|
+ // fileName = fileName + System.currentTimeMillis();
|
|
|
|
|
+ // }
|
|
|
|
|
+
|
|
|
|
|
+ int lastDotIndex = fileName.lastIndexOf(".");
|
|
|
|
|
+ if (lastDotIndex != -1) {
|
|
|
|
|
+ // 存在后缀:前缀_时间戳.后缀
|
|
|
|
|
+ String prefix = fileName.substring(0, lastDotIndex);
|
|
|
|
|
+ String suffix = fileName.substring(lastDotIndex);
|
|
|
|
|
+ fileName = prefix + "_" + System.currentTimeMillis() + suffix;
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+ // 无后缀:原文件名+时间戳
|
|
|
fileName = fileName + System.currentTimeMillis();
|
|
fileName = fileName + System.currentTimeMillis();
|
|
|
}
|
|
}
|
|
|
InputStream in = null;
|
|
InputStream in = null;
|
|
@@ -122,8 +132,7 @@ public class MinioUtils {
|
|
|
.object(fileName)
|
|
.object(fileName)
|
|
|
.stream(in, in.available(), -1)
|
|
.stream(in, in.available(), -1)
|
|
|
.contentType(file.getContentType())
|
|
.contentType(file.getContentType())
|
|
|
- .build()
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ .build());
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
} finally {
|
|
@@ -149,12 +158,25 @@ public class MinioUtils {
|
|
|
public String upload(MultipartFile file) {
|
|
public String upload(MultipartFile file) {
|
|
|
|
|
|
|
|
String fileName = file.getOriginalFilename();
|
|
String fileName = file.getOriginalFilename();
|
|
|
- String[] split = fileName.split("\\.");
|
|
|
|
|
- if (split.length > 1) {
|
|
|
|
|
- fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
|
|
|
|
|
|
|
+ // String[] split = fileName.split("\\.");
|
|
|
|
|
+ // if (split.length > 1) {
|
|
|
|
|
+ // fileName = split[0] + "_" + System.currentTimeMillis() + "." +
|
|
|
|
|
+ // split[split.length - 1];
|
|
|
|
|
+ // } else {
|
|
|
|
|
+ // fileName = fileName + System.currentTimeMillis();
|
|
|
|
|
+ // }
|
|
|
|
|
+
|
|
|
|
|
+ int lastDotIndex = fileName.lastIndexOf(".");
|
|
|
|
|
+ if (lastDotIndex != -1) {
|
|
|
|
|
+ // 存在后缀:前缀_时间戳.后缀
|
|
|
|
|
+ String prefix = fileName.substring(0, lastDotIndex);
|
|
|
|
|
+ String suffix = fileName.substring(lastDotIndex);
|
|
|
|
|
+ fileName = prefix + "_" + System.currentTimeMillis() + suffix;
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+ // 无后缀:原文件名+时间戳
|
|
|
fileName = fileName + System.currentTimeMillis();
|
|
fileName = fileName + System.currentTimeMillis();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
InputStream in = null;
|
|
InputStream in = null;
|
|
|
try {
|
|
try {
|
|
|
in = file.getInputStream();
|
|
in = file.getInputStream();
|
|
@@ -163,8 +185,7 @@ public class MinioUtils {
|
|
|
.object(fileName)
|
|
.object(fileName)
|
|
|
.stream(in, in.available(), -1)
|
|
.stream(in, in.available(), -1)
|
|
|
.contentType(file.getContentType())
|
|
.contentType(file.getContentType())
|
|
|
- .build()
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ .build());
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
} finally {
|
|
@@ -177,7 +198,7 @@ public class MinioUtils {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return this.bucketPath+"/"+this.bucketName+"/"+fileName;
|
|
|
|
|
|
|
+ return this.bucketPath + "/" + this.bucketName + "/" + fileName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -203,8 +224,7 @@ public class MinioUtils {
|
|
|
.object(fileName)
|
|
.object(fileName)
|
|
|
.stream(in, in.available(), -1)
|
|
.stream(in, in.available(), -1)
|
|
|
.contentType(file.getContentType())
|
|
.contentType(file.getContentType())
|
|
|
- .build()
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ .build());
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
} finally {
|
|
} finally {
|
|
@@ -217,7 +237,7 @@ public class MinioUtils {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
String ip = IPUtils.getIpAddr(request);
|
|
String ip = IPUtils.getIpAddr(request);
|
|
|
- return ip +"/"+this.bucketName+"/"+fileName;
|
|
|
|
|
|
|
+ return ip + "/" + this.bucketName + "/" + fileName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -234,7 +254,7 @@ public class MinioUtils {
|
|
|
in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
|
|
in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
|
|
|
out = new ByteArrayOutputStream();
|
|
out = new ByteArrayOutputStream();
|
|
|
IOUtils.copy(in, out);
|
|
IOUtils.copy(in, out);
|
|
|
- //封装返回值
|
|
|
|
|
|
|
+ // 封装返回值
|
|
|
byte[] bytes = out.toByteArray();
|
|
byte[] bytes = out.toByteArray();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
try {
|
|
try {
|
|
@@ -300,9 +320,9 @@ public class MinioUtils {
|
|
|
*/
|
|
*/
|
|
|
public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
|
|
public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
|
|
|
List<DeleteObject> dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
|
|
List<DeleteObject> dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
|
|
|
- Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
|
|
|
|
|
|
|
+ Iterable<Result<DeleteError>> results = minioClient
|
|
|
|
|
+ .removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
|
|
|
return results;
|
|
return results;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|