java通過minio下載pdf附件
一、java通過minio下載pdf附件getObject方法
@Resource
private MinioClient minioClient;
/**
* 通過minio下載pdf附件
* @param fileName:"sdgregrtgfr.pdf" 為存儲(chǔ)在minio中的重命名文件名
* @param originalName:"Java學(xué)習(xí)文檔.pdf" 為實(shí)際文件名
* @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中存儲(chǔ)的路徑
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("附件下載失敗,請(qǐng)重試");
} finally {
inputStream.close();
outputStream.close();
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-857922.html
文章來源:http://www.zghlxwxcb.cn/news/detail-857922.html
到了這里,關(guān)于java通過minio下載pdf附件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!