SpringBoot如何整合minio
作者:OpenWorld
时间:2023-06-09
来源:互联网
浏览:0
首先添加Minio的依赖io.miniominio3.0.10然后写一个controller类这只是一个简单的demo,没有进行任何的封装,可以根据实际情况进行封装。packagecom.file.server.controller;importio.minio.MinioClient;importorg.apache.tomcat.util.http.fileupload.IOUtils;importorg.springframework.web.bind.annotation.*;importorg.
首先添加Minio的依赖
io.minio minio 3.0.10
然后写一个controller类
这只是一个简单的demo,没有进行任何的封装,可以根据实际情况进行封装。
package com.file.server.controller;
import io.minio.MinioClient;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
@RestController
public class MinioController {
private static String url = "http://127.0.0.1:9000"; //minio服务的IP端口
private static String accessKey = "W2ZWITFFDWFM5TWS3WI9";
private static String secretKey = "dNx++XsRJpjmWVQHWv8djMCFJ0A3YXbEr4qfKHR+";
//上传文件到minio服务
@PostMapping("upload")
public String upload(@RequestParam("fileName") MultipartFile file ) {
try {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
InputStream is= file.getInputStream(); //得到文件流
String fileName = file.getOriginalFilename(); //文件名
String contentType = file.getContentType(); //类型
minioClient.putObject("file",fileName,is,contentType); //把文件放置Minio桶(文件夹)
return "上传成功";
}catch (Exception e){
return "上传失败";
}
}
//下载minio服务的文件
@GetMapping("download")
public String download(HttpServletResponse response){
try {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
InputStream fileInputStream = minioClient.getObject("file", "test.jpg");
response.setHeader("Content-Disposition", "attachment;filename=" + "test.jpg");
response.setContentType("application/force-download");
response.setCharacterEncoding("UTF-8");
IOUtils.copy(fileInputStream,response.getOutputStream());
return "下载完成";
}catch (Exception e){
return "下载失败";
}
}
//获取minio文件的下载地址
@GetMapping("url")
public String getUrl(){
try {
MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
String url = minioClient.presignedGetObject("file", "test.jpg");
return url;
}catch (Exception e){
return "获取失败";
}
}
}
作者最新文章
三星 Galaxy A08 渲染图曝光:Helio G99 芯片与 6000mAh 电池配置解析
2026-09-08 17:14
OPPO Find X10 Pro Max 影像规格详解:三颗2亿像素镜头与全焦段8K视频能力
2026-09-08 16:41
PDF转HTML在线转换器怎么选?转换后网页排版怎么查?
2026-09-04 11:02
AE教程书籍挑选指南:零基础、动效与合成方向实战标准
2026-09-02 13:31
教程书籍使用SAI软件Logo要单独授权吗:商标引用与出版合规要点
2026-09-02 11:50
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















