java通过minio下载pdf附件
一、java通过minio下载pdf附件getObject方法
@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();
}
}
文章来源地址https://www.toymoban.com/news/detail-857922.html
文章来源:https://www.toymoban.com/news/detail-857922.html
到了这里,关于java通过minio下载pdf附件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!