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("文件正在被下载");
        }
}
相关推荐
竹叶青_七弦几秒前
从复制粘贴到Harness:Java后端AI编程的工程化之路
java
阿拉斯攀登1 分钟前
MQ消息积压问题排查:消费卡顿、堆积、消费速度优化
后端
摇滚侠1 分钟前
《SpringBoot 3:入门与应用实战》第 14 章 打包与部署 制作 Docker 镜像 阅读笔记 43
spring boot·笔记·docker
wuminyu4 分钟前
Linux内核线程调度与虚拟线程调度机制系统级深度剖析
java·linux·c语言·jvm·c++
阿拉斯攀登6 分钟前
无人售货机库存异步更新方案:解决高并发下单库存卡顿问题
后端
阿拉斯攀登10 分钟前
无人售货机MQ消息丢失、重复消费、超时异常全套兜底方案
后端
做系统的大强13 分钟前
我用中文从零写了一个操作系统(下篇):从45个BUG到165个——假持久化、USB地狱与OS自举
后端
程序员老赵14 分钟前
Docker 部署 Rocky Linux:轻松搭建 RHEL 兼容企业级基础镜像平台
linux·后端·docker
用户61526121321016 分钟前
Java主流框架与源码:Spring Framework
后端
小溪学编程27 分钟前
Java InputStream 详解:从基础到实战
java·python·php