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 ...
随机推荐
- BZOJ 2151 种树
贪心+priority_queue. #include<iostream> #include<cstdio> #include<cstring> #include& ...
- linux下安装虚拟机qemu kqemu
一,为什么要装虚拟机,为什么选择qemu 我的系统里面有3个linux系统,这些系统都是独立的,有的时候,我想一台电脑,能更真实的模拟二台,这个时候我们就可以装个虚拟机.其实如果真的很有钱的话,可能考 ...
- php 格式化数字 位数不足前面加0补足
本文引用自 http://www.fengfly.com/plus/view-62827-1.html 补0: <?php $var = sprintf("%03d", 12 ...
- 七:zookeeper与paxos的分析
zookeeper是什么 官方说辞:Zookeeper 分布式服务框架是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务 ...
- 几种Menu和几种对话框
一.Menu 1.OptionsMenu(弹出菜单) (1)显示弹出菜单布局必须要重写的方法 onCreateOptionsMenu 该方法必须返回true ...
- AsyncTask类
1.定义 异步任务类,在类中实现异步操作,并提供回调方法反馈当前异步执行的程度,最后反馈 的结果提供给UI主线程. <1>Android线程 ...
- 如何定义java中的类
3步走,如下示例代码所示: package com.imooc; //1.定义一个类 public class Telphone { //2.属性(成员变量)有什么 float screen; flo ...
- JVM参数汇总
一.java启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容:其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都满足 ...
- python27+django数据库配置常见问题
mysql缺乏模块,需要安装,建议去http://sourceforge.net/projects/mysql-python/files/mysql-python/下源码编译,或者安装msi文件htt ...
- 淘宝API开发(三)
自动登录到淘宝定时获取订单: C#控制台程序 第一步,获得淘宝真实登录地址.淘宝授权地址(https://oauth.taobao.com/authorize?response_type=token& ...