java通过minio下载pdf附件
文章目录
一、java通过minio下载pdf附件getObject方法
java
@Resource
private MinioClient minioClient;
/**
* 通过minio下载pdf附件
* @param fileName:"sdgregrtgfr.pdf" 为存储在minio中的重命名文件名
* @param originalName:"Java学习文档.pdf" 为实际文件名
* @param response
* @throws IOException
*/
public void getObject(String fileName,String originalName, HttpServletResponse response) throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String filePath = "/";//在minio中存储的路径
GetObjectArgs build = GetObjectArgs.builder().bucket("桶名").object(filePath + fileName).build();
inputStream = minioClient.getObject(build);
response.setContentType("application/pdf;charset=utf-8");
response.setCharacterEncoding("utf-8");
String encodedFileName = URLEncoder.encode(originalName, "UTF-8").replace("+", "%20");
response.setHeader("Content-disposition", "attachment;filename=\"" + encodedFileName + "\"");
outputStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
// 处理异常
e.printStackTrace();
throw new BizException("附件下载失败,请重试");
} finally {
inputStream.close();
outputStream.close();
}
}