文章目录

  • 问题背景
  • 分析
  • 解决方案

错误信息详情:

严重: Servlet.service() for servlet [jsp] in context with path [/exportExcel] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this responseat org.apache.catalina.connector.Response.getWriter(Response.java:678)at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:194)at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)at org.apache.jsp.export_jsp._jspService(export_jsp.java:566)at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)...

问题背景

在写一个 download.jsp ,提供下载文件功能(部分关键代码)如下:

....<%...response.addHeader("Content-Disposition","attachment;filename=\"..\"");response.addHeader("Content-Length",43408);...OutputStream out = response.getOutputStream();InputStream in;//todo intialize inputStream...while((n = in.read(b)) != -1) {out.write(b, 0, n);}out.flush;
%>

运行结果:

结果功能 -> 正常,但是有一点不爽!!日志一直有报错,报错如下:

java.lang.IllegalStateException: getOutputStream() has already been called for this response

分析

翻阅了挺多资料,最后总结如下:

  1. 其实就是因为 JSP 中同时用了 response.getOutputStream() 以及 JSP 内置对象 out.write().
  2. Serlvet 规范中不能同时使用这两个。只能选其中一个。所以再来分析代码:
    1. response.getOutputStream() : 很明显在代码中已经用到了。
    2. out.write():这个很有可能是 JSP 中存在空行或者回车导致的。至于到底是什么时候有 out.write() 。大家可以到 tomcat 目录下看看 jsp 生成的 .java 文件就一目了然了。

解决方案

参考网上的,在调用 response.getOutStream() 之前清空一下两者的缓存。同时更新 out 的内容。

详情看代码(实践已证明可行)↓

<%// 同时 reset response 和 清空 outresponse.reset();out.clear();out = pageContext.pushBody();// ---------------------增加上面的内容--------------------------------------...response.addHeader("Content-Disposition","attachment;filename=\"..\"");response.addHeader("Content-Length",43408);...OutputStream out = response.getOutputStream();InputStream in;//todo intialize inputStream...while((n = in.read(b)) != -1) {out.write(b, 0, n);}out.flush;
%>

喜欢就来点个赞吧~

报错:java.lang.IllegalStateException: getOutputStream() has already been called for this response相关推荐

  1. Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]

    一:运行test类方法时候报错 报错 java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. S ...

  2. 报错 java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @Bootstra

    由于我maven仓库之前已经有了有个5.0.10的test包 导致我的变量 <org.springframework.version>4.2.5.RELEASE</org.sprin ...

  3. 解决java.lang.IllegalStateException: getOutputStream() has already been called for this response

    简单的说:用了流之后关掉即可. 下面详细说明: 出现了java.lang.IllegalStateException: getOutputStream() has already been calle ...

  4. java.lang.IllegalStateException: getOutputStream() has already been called for this response

    在做报表导出的时,导出报表后服务器提示: java.lang.IllegalStateException: getOutputStream() has already been called for ...

  5. 解决Hbase报错java.lang.IllegalStateException: The procedure WAL relies on the ability to hsync for....

    完整报错为: java.lang.IllegalStateException: The procedure WAL relies on the ability to hsync for proper ...

  6. 解决:room无论如何怎么修改数据库版本都报错java.lang.IllegalStateException: Room cannot verify the data integrity

    问题描述 这已经是第二次遇到这个bug了,第一次卡了一两天,这次卡了半天. bug: java.lang.IllegalStateException: Room cannot verify the d ...

  7. Android Studio报错 “java.lang.IllegalStateException: Room cannot verify the data integrity” 解决方法

    最近在Android Studio遇到以下报错: Caused by: java.lang.IllegalStateException: Room cannot verify the data int ...

  8. springboot集成logback,报错java.lang.IllegalStateException: Logback configuration error detected:

    最近需要将一个springboot项目部署到一台新服务器上.而且新服务器是完全copy了旧服务器的环境与项目.但是在新服务器上启动该springboot项目,就一直报错 2019-03-18 18:5 ...

  9. IDEA热布署报错java.lang.IllegalStateException: Restarter has not been initialized

    在IDEA导入devtools依赖实现热布署,启动后遇到如下问题 java.lang.IllegalStateException: Restarter has not been initialized ...

最新文章

  1. webpack-插件机制杂记
  2. 单片机位寻址举例_单片机基础及应用 | 04 80C51单片机指令系统
  3. UbuntuでPostgreSQLをインストールからリモートアクセスまでの手順
  4. 开发环境中实现Lombok消除Java冗余
  5. 如何把你的搜索引擎也加入到Firefox中
  6. PAT甲级1140 Look-and-say Sequence:[C++题解]统计连续个数
  7. 百炼1001: Exponentiation 解题
  8. 多特征值数据预处理_「人工智能」No.6 特征工程之数据预处理
  9. rxjs ThrottleTime 和 debounceTime 的操作符区别
  10. 如何在sqlserver数据库中删除主键相同的数据只留其一
  11. 【渝粤教育】电大中专混凝土结构作业 题库
  12. getBoundingClientRect()
  13. oracle not aviable,ORA-01034: ORACLE not available
  14. Objective C中数组排序几种情况的总结
  15. matlab免疫算法求解配送中心选址问题
  16. 油田智能化远程监控系统_油田远程无线监控系统
  17. oracle alter database,alter database操作
  18. 科普篇:内存、“运行内存”、外存?傻傻分不清楚。看完这篇就懂了
  19. window启动activemq失败
  20. 微信公众号项目(公众号关注登录网站)

热门文章

  1. 软件开发英语单词汇总
  2. 分布式问题,你知道几个?
  3. java毫秒转换参数_SimpleDateFormat使用详解及与毫秒的相互转换
  4. 新业务布局教育,中国奥园能否顺利破圈?
  5. 局域网电脑共享问题, 收藏这一篇教程就够了!!
  6. MATLAB如何得到回文数(用fliplr函数)
  7. DbVisualizer 导出大数据
  8. maven Implementing class
  9. matlab vcredist.msi,vcredist.msi 64位
  10. MIT四足机器人Cheetah 3控制方案理解笔记(1)——摆动腿控制、单刚体模型平衡控制器