小工具知识网

当前位置: 首页 > 行业知识 > java.io.IOException: UT010029: Stream is closed异常

行业知识

java.io.IOException: UT010029: Stream is closed异常

2026-12-05 18:14:00 来源:互联网转载

目录

  • 一、场景
  • 二、异常信息
  • 三、异常分析
  • 四、demo

    一、场景

    后端提供一个excel文件模板下载接口

    ()

    二、异常信息

    java.io.IOException: UT010029: Stream is closedat io.undertow.servlet.spec.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:139) ~[undertow-servlet-2.2.19.Final.jar:2.2.19.Final]at org.springframework.session.web.http.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:624) ~[spring-session-core-2.7.0.jar:2.7.0]at java.io.ByteArrayOutputStream.writeTo(ByteArrayOutputStream.java:167) ~[na:1.8.0_341]

    三、异常分析

    我们使用了ServletOutputStream来向客户端发送响应。在finally块中不需要关闭输出流,因为Servlet容器会在请求处理完成后自动关闭输出流。如果在finally块中尝试关闭已关闭的输出流,就会抛出java.io.IOException: Stream is closed异常。因此,我们可以安全地省略关闭输出流的代码。

    四、demo

        @Operation(summary = "下载模版")    @GetMapping("download")    public Result download(HttpServletResponse response) {        String fileName = "demo模版.xlsx";        ServletOutputStream outputStream = response.getOutputStream()        try {            // 清空下载文件的空白行(空白行是因为有的前端代码编译后产生的)            response.reset();            response.setCharacterEncoding("utf-8");            // 解决中文文件名乱码问题            String encodedFileName = URLEncoder.encode(fileName, "UTF-8");            response.setHeader("Content-Disposition", "attachment;filename=\"" + encodedFileName + "\";filename*=utf-8''" + encodedFileName);            // 设置文件类型            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");            // 读取文件并输出到浏览器            File file = FileUtil.file("templates/demo_templates.xlsx");            outputStream.write(FileUtils.readFileToByteArray(file));            outputStream.flush();        } catch (IOException e) {            log.error("下载文件失败,{}", fileName, e);        } finally {            // 不要在此处关闭输出流            // outputStream.close();        }        return Result.success("下载成功");    }
    ()

上一篇:中国移动应用下载指南

下一篇:云欢云服务器是什么