SysFileServiceImpl.java
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.pashanhoo.qys.service.impl;
import com.pashanhoo.common.minio.FileServerOptUtil;
import com.pashanhoo.common.minio.MinioConfig;
import com.pashanhoo.common.minio.MinioUtil;
import com.pashanhoo.qys.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
/**
* @author CAIYONGSONG
* @commpany www.pashanhoo.com
* @date 2022/7/20
*/
@Service
@Slf4j
public class SysFileServiceImpl implements SysFileService {
@Autowired
private MinioConfig minioConfig;
@Autowired
private MinioUtil minioUtil;
@Override
public String uploadFileMinio(MultipartFile[] files){
Long st = System.currentTimeMillis();
log.info("开始上传批量文件" + files.length);
// 判断存储桶是否存在
if (!minioUtil.bucketExists(minioConfig.getBucketName())) {
minioUtil.makeBucket(minioConfig.getBucketName());
}
StringBuilder sbf = new StringBuilder();
for (MultipartFile file : files) {
if (file.isEmpty()) {
throw new RuntimeException("文件不存在!");
}
log.info("循环开始上传单个文件,文件大小:" + file.getSize());
// 生成文件名 按照要求-带文件夹路径的文件的全路径
String fineName = FileServerOptUtil.extractFilename(file);
try {
// 上传文件
minioUtil.upload(file, fineName, minioConfig.getBucketName());
} catch (Exception e) {
System.out.println("上传minio服务器期出错了 " + e.getMessage());
e.printStackTrace();
return null;
}
sbf.append(minioConfig.getMinioUrl()).append("/").append( minioConfig.getBucketName())
.append("/").append(fineName).append(",");
}
//String url = minioConfig.getMinioUrl() + "/" + minioConfig.getBucketName() + "/" + fineName;
String resUrl = sbf.toString().substring(0,(sbf.length()-1));
log.info("上传文件结束,总耗时:"+(System.currentTimeMillis() - st) +"s, 文件路径:" + resUrl );
return resUrl;
}
@Override
public String uploadFileMinio(MultipartFile file) {
Long st = System.currentTimeMillis();
if (file.isEmpty()) {
throw new RuntimeException("文件不存在!");
}
log.info("开始上传单个文件,文件大小:" + (file.getSize()/1024) + "KB");
// 判断存储桶是否存在
if (!minioUtil.bucketExists(minioConfig.getBucketName())) {
minioUtil.makeBucket(minioConfig.getBucketName());
}
// 生成文件名 按照要求-带文件夹路径的文件的全路径
String fineName = FileServerOptUtil.extractFilename(file);
try {
// 上传文件
minioUtil.upload(file, fineName, minioConfig.getBucketName());
} catch (Exception e) {
System.out.println("上传minio服务器期出错了 " + e.getMessage());
e.printStackTrace();
return null;
}
String url = minioConfig.getMinioUrl() + "/" + minioConfig.getBucketName() + "/" + fineName;
log.info("上传文件结束,总耗时:"+(System.currentTimeMillis() - st) +"ms, 文件路径:" + url );
return url;
}
}