文件下载后台报错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 ...
随机推荐
- Python开发实战PDF
Python开发实战(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1iP9VmwuzDMfdZTfpupR3CA 提取码:a523 复制这段内容后打开百度网盘手机A ...
- 软件工程团队:Spring计划会议及详细计划表
极限挑战! 小组Spring计划表: 11.15 进行软件需求分析,了解调查社会背景,确定要编写的软件,分配各小组成员的任务.确定小组会议每天召开地点时间. 3h 11.16 将任务进一步精确分配, ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习9
#include <iostream>using namespace std;int main(){ int num; cout<<"Enter number of ...
- Nested Dolls 贪心 + dp
G: Nested Dolls Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 99 Solved: 19 Descript ...
- 使用Callable接口创建线程和使用线程池的方式创建线程
1.使用Callable接口的方式实现多线程,这是JDK5.0新增的一种创建多线程的方法 package com.baozi.java2; import java.util.concurrent.Ca ...
- js实现数组去重的几种方法
1.简单结构的数组,例如[1,2,3,3,4],使用es6提供的Set和Array.from Set:是一种新的数据结构,可以接收一个数组或者是类数组对象,自动去重其中的重复项目. 类数组对象:只包含 ...
- angular.isNumber()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- HTML5_提供的 新功能_less 编译_
HTML5_提供的 新功能 class 操作 ele.classList(注意: 高版本的 IE 都不支持) 获取 <div id="ele" class="... ...
- File类中的一些属性 添加删除文件夹
import java.io.File; import java.io.IOException; public class FileD { public static void main(String ...
- F#周报2019年第13期
新闻 Visual Studio 2019发布会 Json2FSharp--在线类型生成器 cs2fs-online--从C#到F#的移植器 AWS Lambda layer上的.NET Core A ...