java通过minio下载pdf附件

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();
        }
    }
相关推荐
剑桥折刀s10 分钟前
Python打卡:Day46
python
巴里巴气32 分钟前
Python爬虫图片验证码和滑块验证码识别总结
爬虫·python
凯基迪科技1 小时前
exe软件壳的分类----加密保护壳
java
wuxuanok1 小时前
Web后端开发-分层解耦
java·笔记·后端·学习
sword devil9001 小时前
PYQT实战:智能家居中控
python·智能家居·pyqt
NetX行者1 小时前
FastMCP:用于构建MCP服务器的开源Python框架
服务器·python·开源
超龄超能程序猿1 小时前
(3)机器学习小白入门 YOLOv: 解锁图片分类新技能
python·numpy·pandas·scipy
kyle~2 小时前
C/C++字面量
java·c语言·c++
waynaqua2 小时前
FastAPI开发AI应用一:实现连续多轮对话
python·openai
纨妙2 小时前
python打卡day59
开发语言·python