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();
        }
    }
相关推荐
SelectDB5 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
Flittly6 小时前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
小兔崽子去哪了6 小时前
Java 生成二维码解决方案
java·后端
人活一口气11 小时前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
NE_STOP13 小时前
Vibe Coding -- 完整项目案例实操
java
荣码13 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
SimonKing13 小时前
Google第三方授权登录
java·后端·程序员
明月光81813 小时前
从一行 @Builder 说起:重新拾起 Java 的 Lombok、注解与 Builder 模式
java
考虑考虑1 天前
Mybatis实现批量插入
java·后端·mybatis
咖啡八杯1 天前
GoF设计模式——中介者模式
java·后端·spring·设计模式