<dependency> <groupid>io.minio</groupid> <artifactid>minio</artifactid> <version>3.0.10</version> </dependency>
然后写一个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;@restcontrollerpublic 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 "获取失败"; } }}
以上就是springboot如何整合minio的详细内容。
