MinIo 安装及其集成到java中上传下载图片信息
win 下安装
下载链接:https://min.io/download#/windows
下载好后cmd打开控制台
输入:minio.exe server D:\MinIo\data
注:D:\MinIo\data 可以自定义盘符目录,存放图片信息
成功启动,输入:127.0.0.1:9000 成功登陆界面(默认账号密码:minioadmin)
登录成功后
linux下安装
下载链接:https://min.io/download#/linux
##赋权限
chmod +x minio
##启动
./minio server /root/minio/data
后台启动
nohup ./minio server /data > minio.log 2>&1 &
安装成功,访问和win下一致;
springboot集成minio
pom.xml
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.1</version>
</dependency>
application.yml
#####################minio文件服务器配置##################################
minio:
endpoint: http://127.0.0.1:9000
accesskey: minioadmin
secretKey: minioadmin
imgBucket: bucketName
注:修改密码密码不能小于8位
上传图片,返回图片存储路径
//filename--图片名称 bucket -- 桶名称(bucketName) serviceUrl--文件服务器地址(http://127.0.0.1:9000)
public String saveImg(MultipartFile file, String bucket, String serviceUrl) {
String fileName = file.getOriginalFilename();
String url = null;
try {
InputStream in = file.getInputStream();
minioClient.putObject(bucket, fileName, in, new PutObjectOptions(in.available(), -1));
in.close();
url = serviceUrl+"/"+bucket+"/"+fileName;
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
下载图片
public void downloadImg(String filename, HttpServletResponse httpResponse) {
try{
//imgBucket--桶名称 filename-- 图片名称
InputStream files = minioClient.getObject(imgBucket, filename);
InputStream ism = new BufferedInputStream(files);
// 调用statObject()来判断对象是否存在。
// 如果不存在, statObject()抛出异常,
// 否则则代表对象存在
minioClient.statObject(imgBucket, filename);
byte buf[] = new byte[1024];
int length = 0;
httpResponse.reset();
//Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。
// Content-disposition其实可以控制用户请求所得的内容存为一个文件的时候提供一个默认的文件名,
// 文件直接在浏览器上显示或者在访问时弹出文件下载对话框。
httpResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
httpResponse.setContentType("application/x-msdownload");
httpResponse.setCharacterEncoding("utf-8");
OutputStream osm = new BufferedOutputStream(httpResponse.getOutputStream());
while ((length = ism.read(buf))>0) {
osm.write(buf,0, length);
}
//关闭流
osm.close();
} catch (MinioException ex) {
ex.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
批量下载成zip
public void downloadPic(String[] files, HttpServletRequest request, HttpServletResponse response) throws IOException {
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
try {
String downloadFilename = "测试.zip";// 文件的名称
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");// 转换中文否则可能会产生乱码
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition","attachment;filename="+downloadFilename);// 设置在下载框默认显示的文件名
//根据名称获取路径信息
for (String file : files) {
String url = minioClient.getObjectUrl(imgBucket, file);
zos.putNextEntry(new ZipEntry(file+ ".jpg"));
InputStream fis = getInputStreamByGet(url);
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
}catch (Exception e) {
e.printStackTrace();
} finally {
zos.flush();
zos.close();
}
}
public static InputStream getInputStreamByGet(String url) {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = conn.getInputStream();
return inputStream;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
删除图片信息
public void delImg(String imgBucket, String imgName, String serviceUrl) {
String url = null;
try {
// 从mybucket中删除myobject。
minioClient.removeObject(imgBucket, imgName);
SysLog.info("successfully removed mybucket/myobject");
url = serviceUrl+"/"+imgBucket+"/"+imgName;
} catch (Exception e) {
System.out.println("Error: " + e);
}
}