struts2 下载时报java.lang.IllegalStateException
-----------------------------------------struts2 下载时报java.lang.IllegalStateException------------------------------------------------------------- 抛出java.lang.IllegalStateException异常,终于发现原因之所在:
我们在做文件上传或者下载,用到页面的输出流. 在action中使用下载的方法:
//文件下载
public String downFile() throws IOException{
String msg=null;
response.setCharacterEncoding("gb2312");
response.setContentType("text/html");
javax.servlet.ServletOutputStream ou = response.getOutputStream(); //文件名
String filename=new String(request.getParameter("filename").getBytes("ISO8859_1"),"utf-8").toString(); //路径
String filepath=ServletActionContext.getServletContext().getRealPath("/upload/"+filename);
java.io.File file = new java.io.File(filepath);
if (!file.exists()) {
System.out.println(file.getAbsolutePath() + " 文件不能存在!");
msg="抱歉,文件可能过期了!";
super.addActionError(msg);
return null;
}
// 读取文件流
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(file); // 下载文件
// 设置响应头和下载保存的文件名
if (filename != null && filename.length() > 0) {
response.setContentType("application/x-msdownload");//弹出下载的框
response.setContentLength((int) file.length());//下载统计文件大小的进度
response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"),"iso8859-1") + "");
//response.setHeader("Content-Length", file.length()); //下载框的信息
if (fileInputStream != null) {
int filelen = fileInputStream.available(); //文件太大时内存不能一次读出,要循环
byte a[] = new byte[filelen];
fileInputStream.read(a);
ou.write(a);
}
fileInputStream.close();
ou.close();
}
return SUCCESS;
}
抛出异常:java.lang.IllegalStateException
原因分析:
这是web容器天生的servlet代码中有out.write(””),
这个和JSP中调用的response.getOutputStream()产生冲突.
即Servlet规范说明,
不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,
在调用第二个时候应会抛出 IllegalStateException,
解决: action方法:
public String downTest(){
try {
name =new String(getFilename().getBytes("iso-8859-1"),"utf-8");
tname=java.net.URLEncoder.encode(name,"utf-8");
String path=ServletActionContext.getServletContext().getRealPath("/upload/"+name);
File file=new File(path);
inputStream=new FileInputStream(file);
response.setContentLength((int) file.length());//下载统计文件大小的进度
} catch (Exception e) {
e.printStackTrace();
} return SUCCESS;
}
struts2.xml:
<!-- 下载中心控制器 -->
<action name="download" class="downloadAction">
<result name="success" type="stream"> <!-- 设置输入流 -->
<param name="inputstream">inputStream</param>
<!-- 设置下载的方式及文件名 -->
<param name="contentDisposition">attachment;filename=${tname}</param>
</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
struts2 下载时报java.lang.IllegalStateException的更多相关文章
- struts2异常记录--java.lang.IllegalStateException
java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFa ...
- 处理eclipse启动时报java.lang.IllegalStateException
这是我写的第一篇博客,博客我来了: 我是好学的人,希望在这上面遇到志同道合的人,对技术有更高追求的人: 重启eclipse的时候报出来 An error has occurred, See the l ...
- Jetty提交数据时报java.lang.IllegalStateException: Form too large270468>200000问题解决
今天在使用Eclipse的Jetty插件做为服务器提交富文本编辑中的数据时,报如下异常: 在\eclipse\plugins目录下,找到org.mortbay.jetty.server_6.1.23. ...
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState问题解决
(1)我用的是fragment,在onStop但是没有onDestroy的情况下切换(replace)fragment时报 java.lang.IllegalStateException: Can n ...
- Eclipse------使用Debug As时报错java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file:/XXX
报错信息: java.lang.IllegalStateException: Failed to read Class-Path attribute from manifest of jar file ...
- 使用RestTemplate时报错java.lang.IllegalStateException: No instances available for 127.0.0.1
我在RestTemplate的配置类里使用了 @LoadBalanced@Componentpublic class RestTemplateConfig { @Bean @LoadBalanced ...
- Spring3.x 版本和 JDK1.8 不兼容导致 java.lang.IllegalStateException: Failed to load ApplicationContext
由于安装了 JDK1.8 的版本,最近在进行整合 Struts2+Spring+Hibernate 框架的时候,不小心导入了之前下载的 Spring 3.2.0 版本的 jar 包. 结果在运行测试用 ...
- 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response
1. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this ...
- java.lang.IllegalStateException: getWriter() has already been called for this response问题解决
java.lang.IllegalStateException: getWriter() has already been called for this response问题解决 java.lang ...
随机推荐
- hdu 1226 超级密码(bfs+余数判重)
题意:略过 分析:用m个数字组成一个能够被n整除的c进制数的最小值,实际上本题的关键就在于这个最小值上. 首先确定我们的思路是从小到大寻找.先查看一位数,即查看着m个数字是否能被n整除:若不能,就查 ...
- myisam 与innodb的区别
myisam 与innodb的区别 frm结构 fri索引 frd数据 innodb:一个表一个文件:frm文件 所有的innodb表,都使用表空间储存, 数据和索引的保存文件不同,myisam 分开 ...
- 20160129.CCPP体系详解(0008天)
程序片段(01):函数.c+call.c+测试.cpp 内容概要:函数 ///函数.c #include <stdio.h> #include <stdlib.h> //01. ...
- 【转】Undefined symbols for architecture i386:和"_OBJC_CLASS_$_xx", referenced from:问题解决方法
多个人共同操作同一个项目或拷贝项目时,经常会出现类似这样的问题: Undefined symbols for architecture i386: "_OBJC_CLASS_$_xx文件名& ...
- c# datagridview表格控件常用操作
1) 行右键菜单 private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { ...
- getHibernateTemplate() 一直报NullPointerException 错误
原来是调用方法有误正确调用方法: ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContex ...
- web-3g-(163)网易-邮箱-记事本-数据库设计
ylbtech-DatabaseDesgin:ylbtech-cnblogs(博客园)-数据库设计-2,Admin(用户后台) DatabaseName:cnblogs(博客园) Model:Admi ...
- [转]linux之date命令
转自:http://www.cnblogs.com/peida/archive/2012/12/13/2815687.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用 ...
- 实现3D摄像机缓冲系统的一些思考
最近需要模拟红侠乔伊的镜头运用.这东西初看简单,实际还是很需要功夫的.关键不是程序技术如何(就一个摄像机),而是分析其轨迹和追踪点规律.其实就是一个3D空间中的缓冲系统.你如何确定都有什么参数,这么多 ...
- C#开源框架(整理)
http://www.cnblogs.com/chinanetwind/p/3715809.html http://www.cnblogs.com/chinanetwind/p/3715813.htm ...