文件下载后台报错IllegalStateException: getOutputStream() has already been called
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的更多相关文章
- Java Web报错:getOutputStream() has already been called for this response解决方案
今天做了个导出excel表的功能.大概代码如下: ouputStream = response.getOutputStream(); wb.write(ouputStream); ouputStrea ...
- 报错记录:getOutputStream() has already been called for this response
仅作记录:参考文章:http://www.blogjava.net/vickzhu/archive/2008/11/03/238337.html 报错信息: java.lang.IllegalStat ...
- java程序后台报错java.net.SocketException: Too many open files
问题描述: 今天一个同事反映程序有问题,让帮忙查看后台日志,发现后台日志报错的信息如下: java.net.SocketException: Too many open files at java.n ...
- MyBatis 分页插件PageHelper 后台报错
今天遇到一个问题,使用MyBatis 分页插件PageHelper 进行排序分页后,能正常返回正确的结果,但后台却一直在报错 net.sf.jsqlparser.parser.ParseExcepti ...
- Weblogic页面应用查询oracle数据库后台报错或页面日期格式显示错误
问题:在生产环境中有两台WEB服务器,分别为227和228,部署的应用代码都是每日同步的,两边完全一致,但是某些页面查询数据时,227无结果,并且后台报java数组越界的错误,而228一切正常.经开发 ...
- java导出excel报错:getOutputStream() has already been called for this response
对于java导出excel报错的问题,查了很多都说是在使用完输出流以后调用以下两行代码即可 out.clear(); out = pageContext.pushBody(); 但这也许是页面上输出时 ...
- robotframework运行时后台报错UnicodeDecodeError
win10环境下报错: Traceback (most recent call last): File "C:\Python27\lib\site-packages\robotide\con ...
- 后台报错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 ...
- 文件下载:报错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 ...
随机推荐
- World Finals 2018 感想
垃圾比赛卡我读入卡我输出毁我青春.
- 马昕璐201771010118《面向对象程序设计(java)》第七周学习总结
第一部分:理论知识学习部分 Java用于控制可见性的4个访问权限修饰符: 1.private(只有该类可以访问) 2.protected(该类及其子类的成员可以访问,同一个包中的类也可访问) 3.pu ...
- ie下js不执行的几种可能
1.首先考虑的就是代码是否有报错2.其次搜索代码中是否有console.log等测试的代码3.检查所用的方法是否兼容ie 以上是鄙人在开发过程中遇到的几种情况,抛砖来引玉~~
- CSS3_综合案例
综合案例 设置元素的 width,还可以利用 left 和 right 为了防止图片太小,background-size: 100% 100%; <!DOCTYPE html> <h ...
- tomcat错误The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
在更换tomcat版本后,原来的项目文件中jsp会出现错误The superclass "javax.servlet.http.HttpServlet" was not found ...
- php 常用正则
1 手机 /^1[34578]\d{9}$/2 qq /^[1-9][0-9]{5,10}$/3 邮箱 /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za ...
- Linux中使用Apache发布html网页
在线学习: https://www.shiyanlou.com/courses/1 工具/原料 Linux,httpd,vi 样例html文件一份 方法/步骤 1 编辑httpd配置文件 2 查找 ...
- python3 load Iris.data数据集出现报错key words: b'Iris-setosa'
通过搜索原因,发现有可能是在对文件读取是编译出现了问题,并且Keyword中提示b'Iris-setosa',而我们的string转float函数中没有字母b,很奇怪.所以尝试将转换函数所有的stri ...
- 关于Mysql6.0+的时区错乱问题
如果使用mysql6.0+的JDBC驱动版本的时候,有时候会出现程序时间与数据库时间相差很多个小时; 1.如果以北京时间为例,相差8个小时的情况一般是你在连接jdbc的url中没有标明system_t ...
- 【Python基础】lpthw - Exercise 39 字典
1. 字典和列表的区别 对于列表,它是一些项的有序排列,只能通过数值来进行索引:对于字典,则可以通过许多东西进行索引,它采用键-值映射的方式,不存在一个特定的顺序,因此不能用类似列表的数值索引,但它的 ...