springboot 集成minio 图片上传、显示
文章目录
pom新增Maven依赖
<!--minio依赖-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
</dependency>
application.yml新增配置
`server:
#端口
port: 8081
#配置minio
minio:
host: http://192.168.1.199:9000
url: ${minio.host}/${minio.bucket}/
access-key: minioadmin
secret-key: minioadmin
bucket: sweet-oa-file
spring:
# 配置文件上传大小限制
servlet:
multipart:
max-file-size: 200MB
max-request-size: 200MB`
编写Minio工具类MinioConfig
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Component
public class MinioConfig implements InitializingBean {
@Value(value = "${minio.bucket}")
private String bucket;
@Value(value = "${minio.host}")
private String host;
@Value(value = "${minio.url}")
private String url;
@Value(value = "${minio.access-key}")
private String accessKey;
@Value(value = "${minio.secret-key}")
private String secretKey;
private MinioClient minioClient;
@Override
public void afterPropertiesSet() throws Exception {
Assert.hasText(url, "Minio url 为空");
Assert.hasText(accessKey, "Minio accessKey为空");
Assert.hasText(secretKey, "Minio secretKey为空");
this.minioClient = new MinioClient(this.host, this.accessKey, this.secretKey);
}
/**
* 上传
*/
public String putObject(MultipartFile multipartFile) throws Exception {
// bucket 不存在,创建
if (!minioClient.bucketExists(this.bucket)) {
minioClient.makeBucket(this.bucket);
}
try (InputStream inputStream = multipartFile.getInputStream()) {
// 上传文件的名称
String fileName = multipartFile.getOriginalFilename();
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
// 文件的ContentType
putObjectOptions.setContentType(multipartFile.getContentType());
minioClient.putObject(this.bucket, fileName, inputStream, putObjectOptions);
// 返回访问路径
return this.url + UriUtils.encode(fileName, StandardCharsets.UTF_8);
}
}
/**
* 文件下载
*/
public void download(String fileName, HttpServletResponse response){
// 从链接中得到文件名
InputStream inputStream;
try {
MinioClient minioClient = new MinioClient(host, accessKey, secretKey);
ObjectStat stat = minioClient.statObject(bucket, fileName);
inputStream = minioClient.getObject(bucket, 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();
System.out.println("有异常:" + e);
}
}
/**
* 检查存储桶是否存在
* @param bucketName
* @return
* @throws Exception
*/
public boolean bucketExists(String bucketName) throws Exception {
boolean flag = minioClient.bucketExists(bucketName);
if (flag) {
return true;
}
return false;
}
/**
* 文件访问路径
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
* @throws Exception
*/
public String getObjectUrl(String bucketName, String objectName) throws Exception {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
url = minioClient.getObjectUrl(bucketName, objectName);
}
return url;
}
}
更新头像图片资源
@ApiOperation(value = "更新用户头像")
@ApiImplicitParams({@ApiImplicitParam(name = "file", value = "头像", dataType = "MultipartFile")})
@PostMapping("/admin/userface")
public RespBean updateHrUserFace(MultipartFile file, Integer id, Authentication authentication) throws Exception {
//1.将头像资源交给minio,返回访问路径
String uploadUrl = minioConfig.putObject(file);
// 2. 头像地址和用户id,交给service进行保存到数据库中。
return adminService.updateAdminUserFace(uploadUrl, id, authentication);
}
修改minio客户端配置