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. composer install 时遇到 Composer\Downloader\TransportException ...

    安装错误 [Composer\Downloader\TransportException] Invalid credentials for 'https://packagist.phpcomposer ...

  2. ajax登录验证-js

    1.html代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  3. __http原理__03__content-type 对照表

    HTTP content-type Content-Type,内容类型,一般是指网页中存在的Content-Type,用于定义网络文件的类型和网页的编码,决定浏览器将以什么形式.什么编码读取这个文件, ...

  4. 理解 Memory barrier(内存屏障)无锁环形队列

    原文:https://www.cnblogs.com/my_life/articles/5220172.html Memory barrier 简介 程序在运行时内存实际的访问顺序和程序代码编写的访问 ...

  5. MySQL中的EXPLAIN

    使用EXPLAIN加上SELECT语句可以获取优化器的查询执行计划 MySQL会在查询上设置一个标记,当执行查询时,这个标记会返回关于在执行计划中每一步的信息,而不是执行它.它会返回一行或多行信息,一 ...

  6. JS生成当前月份包括最近12个月内的月份

    var last_year_month = function() { var result = []; for(var i = 0; i < 12; i++) { var d = new Dat ...

  7. JavaScript基本概念

    JavaScript概念:JavaScript是一个弱类型语言,而且不要进行编译,是解释性语言.JavaScript最初是为了处理一些相较简单的数据验证,从而减少客户端与服务器端的通信提升效率,发展至 ...

  8. composer学习之路01

    以前对composer还是的理解很模糊,直到最近看一些资料,稍微有了一些浅显的了解. /* composer依赖包管理工具,如果一个项目是windows操作系统,那么composer就是360,他可以 ...

  9. 自顶向下深入分析Netty(五)--Future

    再次回顾这幅图,在上一章中,我们分析了Reactor的完整实现.由于Java NIO事件驱动的模型,要求Netty的事件处理采用异步的方式,异步处理则需要表示异步操作的结果.Future正是用来表示异 ...

  10. yum 运行失败

    https://stackoverflow.com/questions/47633870/rpm-lib64-liblzma-so-5-version-xz-5-1-2alpha-not-found- ...