|
|
@@ -15,10 +15,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
+import java.io.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
import java.security.InvalidKeyException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
@@ -86,15 +85,8 @@ public class MinioFileStorageServiceImpl implements MinioFileStorageService {
|
|
|
* @return filePath 文件的路径
|
|
|
*/
|
|
|
@Override
|
|
|
- public String uploadFile(MultipartFile file, String businessType) {
|
|
|
- //获取文件后缀
|
|
|
- String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
|
|
- //获取文件类型
|
|
|
- String fileType = suffix.replace(".", "");
|
|
|
- //获取主键id
|
|
|
- Long uuid = UUID.randomUUID().getMostSignificantBits() & 0x7fffffffffffffffL;
|
|
|
- String filePath = new SimpleDateFormat("yyyy/MM/dd").format(Calendar.getInstance().getTime());
|
|
|
- filePath = "/" + filePath + "/" + businessType + "/" + fileType + "/" + uuid + suffix;
|
|
|
+ public String uploadFile(MultipartFile file, String businessType) throws Exception {
|
|
|
+ String filePath = getFilePath(file, businessType);
|
|
|
try {
|
|
|
minioClient.putObject(
|
|
|
PutObjectArgs
|
|
|
@@ -110,6 +102,45 @@ public class MinioFileStorageServiceImpl implements MinioFileStorageService {
|
|
|
return filePath;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String uploadFileByFile(File file, String businessType) throws Exception {
|
|
|
+ String filePath = getFilePath(file.getName(), businessType);
|
|
|
+ try (InputStream inputStream = Files.newInputStream(file.toPath())) {
|
|
|
+ minioClient.putObject(
|
|
|
+ PutObjectArgs
|
|
|
+ .builder()
|
|
|
+ .bucket(minIOConfigProperties.getBucket())
|
|
|
+ .object(filePath)
|
|
|
+ .stream(inputStream, file.length(), ObjectWriteArgs.MIN_MULTIPART_SIZE)
|
|
|
+ .build()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getFilePath(String file, String businessType) {
|
|
|
+ if(!StringUtils.hasText(file)){
|
|
|
+ throw new RuntimeException("文件名不能为空");
|
|
|
+ }
|
|
|
+ //获取文件后缀
|
|
|
+ String suffix = file.substring(file.lastIndexOf("."));
|
|
|
+ //获取文件类型
|
|
|
+ String fileType = suffix.replace(".", "");
|
|
|
+ //获取主键id
|
|
|
+ long uuid = UUID.randomUUID().getMostSignificantBits() & 0x7fffffffffffffffL;
|
|
|
+ String filePath = new SimpleDateFormat("yyyy/MM/dd").format(Calendar.getInstance().getTime());
|
|
|
+ filePath = "/" + filePath + "/" + businessType + "/" + fileType + "/" + uuid + suffix;
|
|
|
+ return filePath;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getFilePath(MultipartFile file, String businessType) throws Exception {
|
|
|
+ if(file == null){
|
|
|
+ throw new Exception("文件不能为空");
|
|
|
+ }
|
|
|
+ //获取文件后缀
|
|
|
+ return getFilePath(file.getOriginalFilename(), businessType);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<String> uploadFileBatch(List<MultipartFile> files, String businessType) {
|
|
|
List<String> resList = new ArrayList<>();
|