Merge remote-tracking branch 'origin/dev_shb' into develop
This commit is contained in:
@@ -18,16 +18,14 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -39,9 +37,6 @@ import java.util.stream.Collectors;
|
|||||||
public class MinioUtil {
|
public class MinioUtil {
|
||||||
@Autowired
|
@Autowired
|
||||||
private MinioClient minioClient;
|
private MinioClient minioClient;
|
||||||
|
|
||||||
@Value("${minio.bucketName}")
|
|
||||||
private String bucketName;
|
|
||||||
/**
|
/**
|
||||||
* description: 判断bucket是否存在,不存在则创建
|
* description: 判断bucket是否存在,不存在则创建
|
||||||
*
|
*
|
||||||
@@ -94,19 +89,21 @@ public class MinioUtil {
|
|||||||
/**
|
/**
|
||||||
* description: 上传文件
|
* description: 上传文件
|
||||||
*
|
*
|
||||||
|
* @param bucketName
|
||||||
|
* @param path
|
||||||
* @param multipartFile
|
* @param multipartFile
|
||||||
* @return: java.lang.String
|
* @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);
|
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("\\.");
|
String[] split = fileName.split("\\.");
|
||||||
if (split.length > 1) {
|
if (split.length > 1) {
|
||||||
fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];
|
fileName = path + File.separator + UUID.randomUUID() + "." + split[1];
|
||||||
} else {
|
} else {
|
||||||
fileName = fileName + System.currentTimeMillis();
|
fileName = path + File.separator + UUID.randomUUID();
|
||||||
}
|
}
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
try {
|
try {
|
||||||
@@ -134,25 +131,67 @@ public class MinioUtil {
|
|||||||
return names;
|
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: 下载文件
|
* description: 下载文件
|
||||||
*
|
*
|
||||||
* @param fileName
|
* @param path
|
||||||
|
* @param bucketName
|
||||||
* @return: org.springframework.http.ResponseEntity<byte [ ]>
|
* @return: org.springframework.http.ResponseEntity<byte [ ]>
|
||||||
*/
|
*/
|
||||||
public ResponseEntity<byte[]> download(String fileName) {
|
public ResponseEntity<byte[]> download(String path, String bucketName) {
|
||||||
ResponseEntity<byte[]> responseEntity = null;
|
ResponseEntity<byte[]> responseEntity = null;
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
ByteArrayOutputStream out = null;
|
ByteArrayOutputStream out = null;
|
||||||
try {
|
try {
|
||||||
in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
|
in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(path).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 {
|
||||||
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) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user