当前位置: 首页 > article >正文

springboot 文件下载

在springboot中,执行如下代码实现文件下载

    @GetMapping("/file/download/test")
    public void Download(HttpServletResponse response){
        try {
            String path = "XXXXXXXXXXXX";//文件路径
            File file = new File(path);
            // 读到流中
            InputStream inputStream = Files.newInputStream(Paths.get(path));// 文件的存放路径
            response.reset();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
            response.setContentLengthLong(file.length());
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            //从输入流中读取一定数量的字节,并将其存储在缓冲区字节数组中,读到末尾返回-1
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

访问地址,执行下载时,会出错:

No converter for [class cn.hutool.core.io.resource.InputStreamResource] with preset Content-Type ‘application/octet-stream’

对于具有预设内容类型“application/octet流”的[class cn.hutool.core.io.resource.InputStreamResource],没有转换器

当然,在出现错误时,我在想是不是application/octet-stream的问题,也使用过multipart/form-data,但也是一样的结果。

application/octet-stream和multipart/form-data的区别:

或者是这个错误:

org.apache.catalina.connector.ClientAbortException: java.io.IOException: 你的主机中的软件中止了一个已建立的连接。

但是即使会报错,文件还是能完整的下载下来。

找了很久的解决方法,但是没有发现什么正确的答案。

这里贴出来一些别人的解答,供参考思考,本人试过但没成功:

Spring Boot 异常:HttpMessageNotWritableException: No Converter for [class …] With Preset Content-Type - spring 中文网

No converter for XXX with preset Content-Type ‘application/octet-stream;charset=UTF-8‘_unkonwncontenttypeexception-CSDN博客

下载/导出问题(统一返回):No converter for xxx with preset Content-Type ‘application/octet-stream;charset=UTF-8-CSDN博客

这里给出帖主的解决方法:

既然这里访问文件能正确的完成下载,我们就让不进行异常操作:

    @GetMapping("/file/download/test")
    public void Download(HttpServletResponse response){
        try {
            String path = "XXXXXXXXXXXX";//文件路径
            File file = new File(path);
            // 读到流中
            InputStream inputStream = Files.newInputStream(Paths.get(path));// 文件的存放路径
            response.reset();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
            response.setContentLengthLong(file.length());
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            //从输入流中读取一定数量的字节,并将其存储在缓冲区字节数组中,读到末尾返回-1
            while ((len = inputStream.read(b)) > 0) {
                outputStream.write(b, 0, len);
            }
            inputStream.close();
        } catch (IOException e) {
            System.out.println("文件正在被下载");
        }
}


http://www.kler.cn/a/581884.html

相关文章:

  • 带有LBS_OWNERDRAWFIXED 样式的列表框,系统在什么时候向窗口发送WM_DRAWITEM 和WM_MEASUREITEM消息de呢?
  • 使用DeepSeek+蓝耘快速设计网页简易版《我的世界》小游戏
  • 如何在Spring Boot中配置和使用MyBatis-Plus
  • NO.36十六届蓝桥杯备战|位运算和操作符属性|进制转换|原码反码补码|左移|右移|按位与|按位或|按位异或|按位取反(C++)
  • 帕金森病如何 “偷走” 患者的正常生活?
  • HttpMediaTypeNotAcceptableException报错解决,状态码显示为406
  • 3dsmax烘焙光照贴图然后在unity中使用
  • shell脚本一键更新部署docker中服务
  • 《深度学习进阶》第7集:深度实战 通过训练一个智能体玩游戏 来洞察 强化学习(RL)与决策系统
  • 操作系统与网络基础:掌握网络安全的核心技能
  • 基于django+pytorch(Faster R-CNN)的钢材缺陷识别系统
  • HTML 列表详解
  • 【前端】【webpack-dev-server】proxy跨域代理
  • C++全栈聊天项目(2) 单例模式封装Http管理者
  • mac本地部署Qwq-32b记录
  • 【Spring】基础/体系结构/核心模块
  • 计算机网络开发(3)——端口复用、I\O多路复用
  • CSS 中 margin 的margin塌陷问题
  • 图论·拓扑排序
  • ClickHouse 数据倾斜实战:案例分析与优化技巧