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();
        }
    }
相关推荐
阿尔的代码屋27 分钟前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
Re_zero1 小时前
线上日志被清空?这段仅10行的 IO 代码里竟然藏着3个毒瘤
java·后端
洋洋技术笔记1 小时前
Spring Boot条件注解详解
java·spring boot
AI探索者18 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者18 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
程序员清风19 小时前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
FishCoderh20 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅20 小时前
Python函数入门详解(定义+调用+参数)
python
皮皮林55121 小时前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
曲幽21 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama