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. [CF566A]Matching Names

    [CF566A]Matching Names 题目大意: A组和B组各\(n(n\le10^5)\)个字符串\((\sum|S|\le8\times10^5)\),将它们两两匹配,使得每组两个字符串的 ...

  2. ECMA Script 6_RegExp 正则表达式

    在 ES5 中 RegExp 构造函数的参数有两种情况 RegExp(字符串, 正则表达式的修饰符) RegExp(正则表达式); var regex = new RegExp('xyz', 'i') ...

  3. mobile_音悦台

    音悦台 less 可以 width = 1080/67.5rem; /**** 变量定义 ****/ @px: 67.5rem; /**** Start ****/ #wrap { width: 10 ...

  4. bootstrap_开始

    bootstrap 一个移动设备优先 UI 库,底层是用 less 写的,依赖于 jQuery. 面试点: bootstrap 的所有盒子都是怪异盒子模型(box-sizing: border-box ...

  5. [LeetCode] Binary Tree Pruning 二叉树修剪

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

  6. javaweb中的乱码问题(初次接触时写)

    javaweb中的乱码问题 在初次接触javaweb中就遇到了乱码问题,下面是我遇到这些问题的解决办法 1. 页面乱码(jsp) 1. 在页面最前方加上 <%@ page language=&q ...

  7. Windows下自带压缩文件工具之-makecab

    在内网渗透时,当没有rar.7z等压缩工具时候,拖取文件的时候为了防止流量过大,又必须压缩把文件压缩.当然你可以自己上传一个压缩工具.Windows自带制作压缩文件工具makecb你可以了解哈.压缩单 ...

  8. CentOS7.0安装Nginx 1.7.4

    一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...

  9. python模拟---注册登陆查看个人信息

    需求:1.模拟注册: 2.模拟登陆: 3.模拟登陆成功显示登陆成功的用户账号: 一.注册 代码如下: def regetist(): ''' :param username: 注册的账号 :param ...

  10. 2018-2019-2 网络对抗技术 20165311 Exp3 免杀原理与实践

    2018-2019-2 网络对抗技术 20165311 Exp3 免杀原理与实践 免杀原理及基础问题回答 实验内容 任务一:正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil- ...