博客摘录「 文件存储MinIO读写工具类」

 minio现在很多项目在用,Spingboot框架引入包就可以用。

由于我维护的项目是springMVC框架,直接引入会报错,提示包不匹配,各种替换,因为怕影响项目功能,最后决定,单独弄一个SpringMVC的minio,在老项目里调用。代码如下

package com.xdkj.controller;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import com.xdkj.utils.MinioUtil;
import com.xdkj.utils.MinioUtils;

import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;

import io.minio.UploadObjectArgs;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Arrays;


/**
* @author joke
* @version 1.0
* @date 2021/11/25-22:11
*/
@Controller
public class UtilController {

private static String endpoint ="http://192.168.1.184:9000";
private static String accessKey="minioadmin";
private static String secretKey="minioadmin";


/**
* 档案系统定时器执行此方法,根据推送的附件地址,将附件下载到minio服务器,用于后续归档
* @param
* @return
* @throws Exception
*/

@PostMapping("/bucketExists")
@ResponseBody
public String upload(HttpServletRequest request) throws Exception {
String data= ServletUtil.getBody(request);
System.out.println(data);
JSONObject json = JSONUtil.parseObj(data);
String bucketName=json.get("bucketName").toString(); //桶名
String url=json.get("url").toString();//附件地址
String ProjectNo=json.get("ProjectNo").toString();//附件地址
String uploadPath=json.get("uploadPath").toString();
String objectName="URLFile/"+ProjectNo+"/";
String res="";
MinioUtils util=new MinioUtils(endpoint, accessKey,secretKey );
boolean exists= util.bucketExists(bucketName); //是否存在桶
if(exists){ //存在的话,通过地址下载附件
ObjectWriteResponse aa=util.putDirObject(bucketName, objectName); //创建目录
util.netToMinio(url, bucketName,objectName, uploadPath) ; //上传附件
}else{
util.createBucket(bucketName);
ObjectWriteResponse aa=util.putDirObject(bucketName, objectName);
util.netToMinio(url, bucketName,ProjectNo,uploadPath) ;
}
return res;
}

/**
* 将minio附件下载到本地
* @param request
* @param response
*/

@PostMapping("/getObject")
@ResponseBody
public void getObject(HttpServletRequest request,HttpServletResponse response) throws Exception {
String data= ServletUtil.getBody(request);
System.out.println(data);
MinioUtils util=new MinioUtils(endpoint, accessKey,secretKey);
JSONObject json = JSONUtil.parseObj(data);
// String bucketName=json.get("bucketName").toString();
String objectName= json.get("objectName").toString() ;
String fileName= json.get("fileName").toString();
System.out.println("所有桶名"+ util.getAllBuckets());
System.out.println("objectName==========="+ objectName);
System.out.println("fileName==========="+ fileName);
boolean aa =util.doesObjectExist (objectName);
System.out.println("aa"+"busunc"+aa);
util.downloadObject("data",objectName,fileName);
System.out.println("文件已下载:");
}




/**
* 下载文件
* @param
* @param response
* @return
*/
@PostMapping("/downloadFile")
@ResponseBody
public String downloadFile( HttpServletRequest request, HttpServletResponse response) {
String data= ServletUtil.getBody(request) ;
System.out.println(data);
MinioUtils minioClient=new MinioUtils(endpoint, accessKey,secretKey);
JSONObject json = JSONUtil.parseObj(data);
String fileName= json.get("fileName").toString();
if (minioClient == null) {
return "连接MinIO服务器失败";
}

return minioClient.downloadFile("data",fileName,response) != null ? "下载成功" : "下载失败";
}


/**
* 将本地文件上传到minio
* @param request
* @throws Exception
*/
@PostMapping("/putObject")
@ResponseBody
public void putObject(HttpServletRequest request) throws Exception {
String data= ServletUtil.getBody(request) ;
System.out.println(data);
MinioUtils minioClient=new MinioUtils(endpoint, accessKey,secretKey);
JSONObject json = JSONUtil.parseObj(data);
String fileName= json.get("fileName").toString();
String localPath= json.get("localPath").toString();
ObjectWriteResponse aa=minioClient.putDirObject("data", "electronicDocuments/"); //创建目录
minioClient.putObject("data", fileName, localPath);
}


/**
* 获取文件大小
* @param request
* @param response
* @return
* @throws Exception
*/

@PostMapping("/getFileSize")
@ResponseBody
public long getFileSize( HttpServletRequest request, HttpServletResponse response) throws Exception {
String data= ServletUtil.getBody(request) ;
System.out.println(data);
MinioUtils minioClient=new MinioUtils(endpoint, accessKey,secretKey);
JSONObject json = JSONUtil.parseObj(data);
String fileName= json.get("fileName").toString();
return minioClient.getFileSize(fileName,"data") ;
}

/**
* 下载文件
*
* @param fileName 文件名称
* @return {@link ResponseEntity}<{@link byte[]}>
// */
@GetMapping("/downloadFileWD")
public ResponseEntity downloadFileWD(@RequestParam String fileName) {
ResponseEntity responseEntity = null;
InputStream in = null;
ByteArrayOutputStream out = null;
MinioClient minioClient = MinioUtil.getClient();
try {
in = minioClient.getObject(GetObjectArgs.builder()
.bucket("data")
.object("electronicDocuments/"+fileName)
.build());
out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
//封装返回值
byte[] bytes = out.toByteArray();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName));
headers.setContentLength(bytes.length);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setAccessControlExposeHeaders(Arrays.asList("*"));
responseEntity = new ResponseEntity(bytes, headers, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseEntity;
}


}

你可能感兴趣的:(笔记,java,spring)