package com.panzhihua.grid_app.config;
|
|
import io.minio.MinioClient;
|
import io.minio.ObjectStat;
|
import org.apache.commons.io.IOUtils;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.net.URLEncoder;
|
import java.util.UUID;
|
|
/**
|
* program 攀枝花智慧社区项目 description minio工具类
|
*
|
* @author manailin Date 2021-08-19 15:30
|
**/
|
@Component
|
@EnableConfigurationProperties(MinioConfig.class)
|
public class MinioUtil {
|
|
@Resource
|
private MinioConfig minioProperties;
|
|
public static byte[] toByteArray(InputStream input) throws IOException {
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
byte[] buffer = new byte[4096];
|
int n = 0;
|
while (-1 != (n = input.read(buffer))) {
|
output.write(buffer, 0, n);
|
}
|
return output.toByteArray();
|
}
|
|
/**
|
* 文件上传
|
*
|
* @param file
|
* 要上传的文件
|
* @return
|
*/
|
public String upload(MultipartFile file, String name) {
|
if (null != file) {
|
try {
|
UUID uuid = UUID.randomUUID();
|
StringBuilder s = new StringBuilder();
|
s.append(uuid.toString().replace("-", "")).append("/");
|
MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(),
|
minioProperties.getSecretKey());
|
// bucket 不存在,创建
|
if (!minioClient.bucketExists(minioProperties.getBucket())) {
|
minioClient.makeBucket(minioProperties.getBucket());
|
}
|
// 得到文件流
|
InputStream input = file.getInputStream();
|
// 文件名
|
// String fileName = uuid + "/images." + FilenameUtils.getExtension(file.getOriginalFilename());
|
String fileName = s.append(name).toString();
|
String contentType = file.getContentType();
|
minioClient.putObject(minioProperties.getBucket(), fileName, input, contentType);
|
StringBuilder fileUrl = new StringBuilder(minioProperties.getUrl());
|
String url = fileUrl.append(fileName).toString();
|
return url;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 文件下载
|
*
|
* @param response
|
* @param url
|
*/
|
public void download(HttpServletResponse response, String url) {
|
// 从链接中得到文件名
|
String replace = url.replace(minioProperties.getBucket() + "/", "#");
|
String fileName = replace.split("#")[1];
|
InputStream inputStream;
|
try {
|
MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(),
|
minioProperties.getSecretKey());
|
ObjectStat stat = minioClient.statObject(minioProperties.getBucket(), fileName);
|
inputStream = minioClient.getObject(minioProperties.getBucket(), fileName);
|
response.setContentType(stat.contentType());
|
response.setCharacterEncoding("UTF-8");
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
IOUtils.copy(inputStream, response.getOutputStream());
|
inputStream.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
/**
|
* 文件下载
|
*
|
* @param response
|
* @param url
|
*/
|
public byte[] getBytes(HttpServletResponse response, String url) {
|
// 从链接中得到文件名
|
String replace = url.replace(minioProperties.getBucket() + "/", "#");
|
String fileName = replace.split("#")[1];
|
InputStream inputStream;
|
byte[] bytes = new byte[0];
|
try {
|
MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(),
|
minioProperties.getSecretKey());
|
inputStream = minioClient.getObject(minioProperties.getBucket(), fileName);
|
bytes = toByteArray(inputStream);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return bytes;
|
}
|
}
|