TASK:workspace、design模块代码;
This commit is contained in:
@@ -18,16 +18,14 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.*;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -39,9 +37,6 @@ import java.util.stream.Collectors;
|
||||
public class MinioUtil {
|
||||
@Autowired
|
||||
private MinioClient minioClient;
|
||||
|
||||
@Value("${minio.bucketName}")
|
||||
private String bucketName;
|
||||
/**
|
||||
* description: 判断bucket是否存在,不存在则创建
|
||||
*
|
||||
@@ -94,19 +89,21 @@ public class MinioUtil {
|
||||
/**
|
||||
* description: 上传文件
|
||||
*
|
||||
* @param bucketName
|
||||
* @param path
|
||||
* @param multipartFile
|
||||
* @return: java.lang.String
|
||||
|
||||
*/
|
||||
public List<String> upload(MultipartFile[] multipartFile) {
|
||||
public List<String> uploadBatch(String bucketName, String path, MultipartFile[] multipartFile) {
|
||||
List<String> names = new ArrayList<>(multipartFile.length);
|
||||
for (MultipartFile file : multipartFile) {
|
||||
String fileName = file.getOriginalFilename();
|
||||
String[] split = fileName.split("\\.");
|
||||
if (split.length > 1) {
|
||||
fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
|
||||
fileName = path + File.separator + UUID.randomUUID() + "." + split[1];
|
||||
} else {
|
||||
fileName = fileName + System.currentTimeMillis();
|
||||
fileName = path + File.separator + UUID.randomUUID();
|
||||
}
|
||||
InputStream in = null;
|
||||
try {
|
||||
@@ -134,25 +131,67 @@ public class MinioUtil {
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* description: 上传文件
|
||||
*
|
||||
* @param bucketName
|
||||
* @param path
|
||||
* @param file
|
||||
* @return: java.lang.String
|
||||
|
||||
*/
|
||||
public String upload(String bucketName, String path, MultipartFile file) {
|
||||
String fileName = file.getOriginalFilename();
|
||||
String[] split = fileName.split("\\.");
|
||||
if (split.length > 1) {
|
||||
fileName = path + File.separator + UUID.randomUUID() + "." + split[1];
|
||||
} else {
|
||||
fileName = path + File.separator + UUID.randomUUID();
|
||||
}
|
||||
InputStream in = null;
|
||||
try {
|
||||
in = file.getInputStream();
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(fileName)
|
||||
.stream(in, in.available(), -1)
|
||||
.contentType(file.getContentType())
|
||||
.build()
|
||||
);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* description: 下载文件
|
||||
*
|
||||
* @param fileName
|
||||
* @param path
|
||||
* @param bucketName
|
||||
* @return: org.springframework.http.ResponseEntity<byte [ ]>
|
||||
*/
|
||||
public ResponseEntity<byte[]> download(String fileName) {
|
||||
public ResponseEntity<byte[]> download(String path, String bucketName) {
|
||||
ResponseEntity<byte[]> responseEntity = null;
|
||||
InputStream in = null;
|
||||
ByteArrayOutputStream out = null;
|
||||
try {
|
||||
in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
|
||||
in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(path).build());
|
||||
out = new ByteArrayOutputStream();
|
||||
IOUtils.copy(in, out);
|
||||
//封装返回值
|
||||
byte[] bytes = out.toByteArray();
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
try {
|
||||
headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||
headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(path, "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user