|
@@ -10,6 +10,9 @@ import java.io.File;
|
10
|
10
|
import java.io.FileInputStream;
|
11
|
11
|
import java.io.IOException;
|
12
|
12
|
import java.io.InputStream;
|
|
13
|
+import java.time.LocalDate;
|
|
14
|
+import java.time.LocalDateTime;
|
|
15
|
+import java.time.LocalTime;
|
13
|
16
|
import java.util.UUID;
|
14
|
17
|
import java.util.concurrent.Callable;
|
15
|
18
|
|
|
@@ -49,18 +52,22 @@ public class AliOSSUtils {
|
49
|
52
|
|
50
|
53
|
public String upload(MultipartFile file) throws IOException {
|
51
|
54
|
// 获取上传的文件的输入流
|
52
|
|
- InputStream inputStream = file.getInputStream();
|
53
|
|
- // 避免文件覆盖
|
54
|
|
- String originalFilename = file.getOriginalFilename();
|
55
|
|
- String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
|
56
|
|
- //上传文件到 OSS
|
57
|
|
- OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
|
58
|
|
- threadPoolUtil.getTaskExecutor().execute(() -> {
|
59
|
|
- ossClient.putObject(aliProperties.getBucketName(), fileName, inputStream);
|
60
|
|
- ossClient.shutdown();
|
61
|
|
- });
|
62
|
|
- //文件访问路径
|
63
|
|
- return aliProperties.getEndpoint().split("//")[0] + "//" + aliProperties.getBucketName() + "." + aliProperties.getEndpoint().split("//")[1] + "/" + fileName;
|
|
55
|
+ try {
|
|
56
|
+ InputStream inputStream = file.getInputStream();
|
|
57
|
+ // 避免文件覆盖
|
|
58
|
+ String originalFilename = file.getOriginalFilename();
|
|
59
|
+ String fileName = "file/" + LocalDate.now() + "/" + UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
60
|
+ //上传文件到 OSS
|
|
61
|
+ OSS ossClient = new OSSClientBuilder().build(aliProperties.getEndpoint(), aliProperties.getAccessKeyId(), aliProperties.getAccessKeySecret());
|
|
62
|
+ threadPoolUtil.getTaskExecutor().execute(() -> {
|
|
63
|
+ ossClient.putObject(aliProperties.getBucketName(), fileName, inputStream);
|
|
64
|
+ ossClient.shutdown();
|
|
65
|
+ });
|
|
66
|
+ //文件访问路径
|
|
67
|
+ return aliProperties.getEndpoint().split("//")[0] + "//" + aliProperties.getBucketName() + "." + aliProperties.getEndpoint().split("//")[1] + "/" + fileName;
|
|
68
|
+ } catch (IOException e) {
|
|
69
|
+ throw new RuntimeException(e);
|
|
70
|
+ }
|
64
|
71
|
}
|
65
|
72
|
|
66
|
73
|
|