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();
        }
    }
相关推荐
华仔啊5 小时前
SpringBoot 中 6 种数据脱敏方案,第 5 种太强了,支持深度递归!
java·后端
异常驯兽师6 小时前
Spring 中处理 HTTP 请求参数注解全解析
java·spring·http
Y学院6 小时前
Python 数据分析:从新手到高手的“摸鱼”指南
python·数据分析
连合机器人6 小时前
晨曦中的守望者:当科技为景区赋予温度
java·前端·科技
深耕AI6 小时前
【PyTorch训练】准确率计算(代码片段拆解)
人工智能·pytorch·python
AD钙奶-lalala6 小时前
idea新建的项目new 没有java class选项
java·ide·intellij-idea
eqwaak06 小时前
科技信息差(9.12)
开发语言·python·科技·量子计算
sheji34167 小时前
【开题答辩全过程】以 12306候补购票服务系统为例,包含答辩的问题和答案
java·eclipse
Blossom.1187 小时前
从“能写”到“能干活”:大模型工具调用(Function-Calling)的工程化落地指南
数据库·人工智能·python·深度学习·机器学习·计算机视觉·oracle