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

 <%@page language="java" contentType="text/html; charset=UTF-8"%>
<%@page import="java.util.*,java.io.*"%>
<html>
<head>
<title>下载页面</title>
</head> <body>
<%
response.reset(); //记住要记住reset浏览器清空缓存,否则可能会出现乱码情况 OutputStream o=response.getOutputStream(); ResourceBundle res = ResourceBundle.getBundle("test"); //test.properties
String XLSFile = res.getString("tempFileRootPath") + "/excel";
File fileLoad=new File(XLSFile,"temp.xls"); if(fileLoad.exists()) {
response.setHeader("Content-disposition","attachment;filename="+URLEncoder.encode("filename文件名", "UTF-8")+".xls");//文件中文名UTF-8
response.setContentType("application/vnd.ms-excel");
long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length); FileInputStream in = new FileInputStream(fileLoad);
OutputStream o = null;
try{
in = new FileInputStream(fileLoad); o = response.getOutputStream();
out.clear();
out = pageContext.pushBody();
// int n=0;
byte b[]=new byte[500]; while((n=in.read(b))!=-1) {
o.write(b,0,n);
}
o.flush(); } catch (Exception e) {
out.write("文件导出异常" + e.toString());
} finally {
if(in != null) {
try {
in.close();
} catch(Exception e) {}
}
if(o != null) {
try {
o.close();
} catch(Exception e) {}
}
}
} else {
out.write("未找到导出的临时文件");
} %> </body>
</html>

原因:

在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后
有一段这样的代码
finally {
   if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
  }
这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和
response.getOutputStream()相冲突的!所以会出现以上异常 IllegalStateException: getOutputStream() has already been called。
jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。

解决方法:在使用完response.getOutputStream()的后面加上两句(标黄):

out.clear();
out = pageContext.pushBody();

附上javax.servlet.jsp.PageContext方法pushBody() 说明:

public BodyContent pushBody()
Return a new BodyContent object, save the current "out" JspWriter, and update the value of the "out" attribute in the page scope attribute namespace of the PageContext.
Returns:
the new BodyContent

文件下载后台报错IllegalStateException: getOutputStream() has already been called的更多相关文章

  1. Java Web报错:getOutputStream() has already been called for this response解决方案

    今天做了个导出excel表的功能.大概代码如下: ouputStream = response.getOutputStream(); wb.write(ouputStream); ouputStrea ...

  2. 报错记录:getOutputStream() has already been called for this response

    仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...

  3. java程序后台报错java.net.SocketException: Too many open files

    问题描述: 今天一个同事反映程序有问题,让帮忙查看后台日志,发现后台日志报错的信息如下: java.net.SocketException: Too many open files at java.n ...

  4. MyBatis 分页插件PageHelper 后台报错

    今天遇到一个问题,使用MyBatis 分页插件PageHelper 进行排序分页后,能正常返回正确的结果,但后台却一直在报错 net.sf.jsqlparser.parser.ParseExcepti ...

  5. Weblogic页面应用查询oracle数据库后台报错或页面日期格式显示错误

    问题:在生产环境中有两台WEB服务器,分别为227和228,部署的应用代码都是每日同步的,两边完全一致,但是某些页面查询数据时,227无结果,并且后台报java数组越界的错误,而228一切正常.经开发 ...

  6. java导出excel报错:getOutputStream() has already been called for this response

    对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...

  7. robotframework运行时后台报错UnicodeDecodeError

    win10环境下报错: Traceback (most recent call last): File "C:\Python27\lib\site-packages\robotide\con ...

  8. 后台报错java.lang.IllegalArgumentException: Invalid character found in the request target.

    报错: Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level. java.lang ...

  9. 文件下载:报错The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'

    前言:这篇文件下载的后台代码太繁琐,建议参考https://www.cnblogs.com/zwh0910/p/13745947.html 前端: <el-button type="p ...

随机推荐

  1. linux(debian) arm-linux-g++ v4.5.1交叉编译 embedded arm 版本的QtWebkit (browser) 使用qt 4.8.6 版本 以及x64上编译qt

    最近需要做一个项目 在arm 架构的linux下 没有桌面环境的情况下拉起 有界面的浏览器使用. 考虑用qt 的界面和 qtwebikt 的库去实现这一系列操作. 本文参考: Qt移植到ARM Lin ...

  2. HTML5_图片合成_刮刮卡

    刮刮卡(图片合成) 定义: globalCompositeOperation 属性,设置或返回如何将源图像 将 myCanvas 的背景图设置为一张图片,(刮开后显示) // 目标图像(已有的,外面一 ...

  3. python 格式化向sql语句输出元组

    想要往执行的sql语句里传入元组怎么办?尤其像insert 数据等操作,其实 利用"{}".format(tuple)固定格式传值即可 results = ((,,),(,,)) ...

  4. ASCII编码查看

    实例说明 ASCII是American Standard Code Information Interchange的缩写,是基于拉丁字母的一套电脑编码系统,主要用于显示英文字符,是目前世界上最通用的单 ...

  5. SSH集成(Struts+Spring+Hibernate)

    环境:struts2.3.Xspring4.0.0hibernate4.2 思路:从下开始往上集成;层与层之间没有关系;在集成的时候,只关注当前集成的那个层的内容; 1,创建一个空的web项目;重新定 ...

  6. CentOS启动docker1.13失败(Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.)

    一.启动失败 1.启动docker [root@localhost ~]# systemctl start docker Job for docker.service failed because t ...

  7. Express全系列教程之(四):获取Post参数的两种方式

    一.关于POST请求 post方法作为http请求很重要的一部分,几乎所有的网站都有用到它,与get不同,post请求更像是在服务器上做修改操作,它一般用于数据资源的更新.相比于get请求,post所 ...

  8. Spring boot 国际化自动加载资源文件问题

    Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...

  9. Web Application Security(Web应用安全)

    Web Application Security 1.web应用面临的主要安全问题 1)黑客入侵:撞库拖库.网页篡改.后门木马.加密勒索.数据泄露 2)恶意内容 2.web应用安全现状 1)网站安全问 ...

  10. 在VM虚拟机中安装Centos操作系统

    首先我们要下载  Centos https://www.centos.org/ 这个是Centos官方 最新版本 7 https://www.centos.org/download/ 提供有 DVD安 ...