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 ...
随机推荐
- IOS中UIWebView执行javaScript脚本时注意点
1.webView之所以能够滚动,因为它内部有一个UIScrollView子控件 2.移除webView顶部和底部灰色的一层view * 遍历webView中scrollView内部的所有子控件 * ...
- 浅析extendedLayout, automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars
参考文章: http://stackoverflow.com/questions/18798792/explaining-difference-between-automaticallyadjusts ...
- laravel5 centos6.4下的配置体验
1. 安装lmnp环境: nginx version: nginx/1.6.0. php 5.5.7 . centos6.42. laravel-v5.1.4 一键安装包,在使用composer 安装 ...
- (三)spark集群DHCP IP变化后的处理
学校的网比较特殊(本人比较菜),需要DHCP获得地址,那么有时候IP被占用了,应该如何应对呢? 1)修改/etc/hosts 2) 修改spark-env.sh 里的 Master 地址 ,Maste ...
- MySQL表类型
学习Mysql数据库,Mysql表类型都有哪些是一定需要知道的,下面就为您介绍七种Mysql表类型,希望能对您学习Mysql表类型有所帮助. MySQL作为当前最为流行的免费数据库服务引擎,已经风靡了 ...
- Excel 之查找与替换
Excel查找与替换 1,一旦学会查找,替换就简单了. 2,查找下面有一个选项,里面有对查找的范围进行限制 3,你可以选中一个区域,然后再查找,查找只会在你选择的区域里面进行 4,查找只能找特定值,而 ...
- mouseevent tips
关于roll_over 和 mouse_over的区别,这篇文章说明的很清楚,http://zengrong.net/post/1105.htm 全文如下: 在MouseEvent中,ROLL_OVE ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.6. 安装Oracle所依赖的必要包
2.6. 安装Oracle所依赖的必要包 2.6.1. 检查Oracle所依赖的必要rpm包 [root@localhost /]#rpm -q binutils compat-libstdc elf ...
- 【PHP入门到精通】:Ch03:PHP语言基础
1, PHP风格 这里为了显示代码把"<"和">"和key值以空格分开了,实际书写时切记不要将其分开: (1) < ?php ? >: ...
- [转] C# 绘制报表,使用Graphics.DrawString 方法
原文 Graphics.DrawString 方法 在指定位置并且用指定的 Brush 和Font 对象绘制指定的文本字符串. public void DrawString( string s, Fo ...