package com.gkhy.safePlatform.minioDemo.utils;
|
|
import com.gkhy.safePlatform.minioDemo.config.MinioConfig;
|
import io.minio.*;
|
import io.minio.MinioClient;
|
import io.minio.errors.*;
|
import io.minio.http.Method;
|
import lombok.SneakyThrows;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.nio.charset.StandardCharsets;
|
import java.security.InvalidKeyException;
|
import java.security.NoSuchAlgorithmException;
|
|
@Configuration
|
public class FileUtil {
|
|
@Resource
|
private MinioConfig minioConfig;
|
|
@Autowired
|
private MinioClient minioClient;
|
|
|
/**
|
* 文件下载
|
* @param fileName
|
* @param response
|
*/
|
public void downloadFile(String fileName, HttpServletResponse response) {
|
try {
|
InputStream file = minioClient.getObject(GetObjectArgs.builder()
|
.bucket(minioConfig.getBucketName())
|
.object(fileName)
|
.build());
|
String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
|
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
|
ServletOutputStream servletOutputStream = response.getOutputStream();
|
int len;
|
byte[] buffer = new byte[1024];
|
while ((len = file.read(buffer)) > 0) {
|
servletOutputStream.write(buffer, 0, len);
|
}
|
servletOutputStream.flush();
|
file.close();
|
servletOutputStream.close();
|
} catch (ErrorResponseException e) {
|
e.printStackTrace();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
/**
|
* 文件上传
|
*
|
* @param file 文件
|
* @param fileName 存储的文件名称
|
*/
|
public String upload(MultipartFile file, String fileName) {
|
String url;
|
try {
|
PutObjectArgs objectArgs = PutObjectArgs.builder()
|
.bucket(minioConfig.getBucketName())
|
.object(fileName)
|
.stream(file.getInputStream(), file.getSize(), -1)
|
.contentType(file.getContentType())
|
.build();
|
//文件名称相同会覆盖
|
minioClient.putObject(objectArgs);
|
url = getFileUrl(fileName);
|
} catch (Exception e) {
|
e.printStackTrace();
|
url = "";
|
}
|
return url;
|
}
|
|
/**
|
* 文件访问路径
|
*
|
* @param objectName 存储桶里的对象名称
|
* expiry :链接失效时间,秒为单位
|
* @return
|
*/
|
public String getFileUrl(String objectName) {
|
String url = null;
|
try {
|
url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
|
.method(Method.GET)
|
.bucket(minioConfig.getBucketName())
|
.object(objectName)
|
.expiry(60).build());
|
} catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | XmlParserException | ServerException e) {
|
e.printStackTrace();
|
}
|
return url;
|
}
|
|
|
/**
|
* 检查存储桶是否存在
|
*
|
* @return
|
*/
|
@SneakyThrows
|
public boolean bucketCheck() {
|
boolean flag = minioClient.bucketExists(BucketExistsArgs.builder()
|
.bucket(minioConfig.getBucketName()).build());
|
return flag;
|
}
|
|
}
|