-----------------------------------------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的更多相关文章

  1. struts2异常记录--java.lang.IllegalStateException

    java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFa ...

  2. 处理eclipse启动时报java.lang.IllegalStateException

    这是我写的第一篇博客,博客我来了: 我是好学的人,希望在这上面遇到志同道合的人,对技术有更高追求的人: 重启eclipse的时候报出来 An error has occurred, See the l ...

  3. Jetty提交数据时报java.lang.IllegalStateException: Form too large270468>200000问题解决

    今天在使用Eclipse的Jetty插件做为服务器提交富文本编辑中的数据时,报如下异常: 在\eclipse\plugins目录下,找到org.mortbay.jetty.server_6.1.23. ...

  4. java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState问题解决

    (1)我用的是fragment,在onStop但是没有onDestroy的情况下切换(replace)fragment时报 java.lang.IllegalStateException: Can n ...

  5. 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 ...

  6. 使用RestTemplate时报错java.lang.IllegalStateException: No instances available for 127.0.0.1

    我在RestTemplate的配置类里使用了 @LoadBalanced@Componentpublic class RestTemplateConfig { @Bean @LoadBalanced ...

  7. Spring3.x 版本和 JDK1.8 不兼容导致 java.lang.IllegalStateException: Failed to load ApplicationContext

    由于安装了 JDK1.8 的版本,最近在进行整合 Struts2+Spring+Hibernate 框架的时候,不小心导入了之前下载的 Spring 3.2.0 版本的 jar 包. 结果在运行测试用 ...

  8. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this response

    1. 用java实现文件下载,提示java.lang.IllegalStateException: getOutputStream() has already been called for this ...

  9. java.lang.IllegalStateException: getWriter() has already been called for this response问题解决

    java.lang.IllegalStateException: getWriter() has already been called for this response问题解决 java.lang ...

随机推荐

  1. IOS中UIWebView执行javaScript脚本时注意点

    1.webView之所以能够滚动,因为它内部有一个UIScrollView子控件 2.移除webView顶部和底部灰色的一层view * 遍历webView中scrollView内部的所有子控件 * ...

  2. python练习程序(c100经典例17)

    题目: 输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. def foo(a): l=len(a); letters=0; space=0; digit=0; others=0; f ...

  3. mysql关于列转行的想法,以及列求乘集

    mysql列转行可以通过concat,先分组然后连接. show VARIABLES like '%group%' select @@group_concat_max_len SELECT GROUP ...

  4. swun 1766 我的悲剧不可能那么好数

    解题思路: 一向提交特别慎重的我,这题竟然PE了5发左右,放了几天,再回来写,直接1A, 相当的自豪,而且是最优解题者.这题千万要注意,化繁为简,文章只包括大小   写字母和数字,还有空行. #inc ...

  5. Sqoop的使用(Mysql To HBase)

    最近需要将mysql的数据整合到HBase中,原本使用MapReduce,自己制作job将mysql的数据导入, 查阅资料过程中,发现了开源工具sqoop(关系性数据库与HDFS,HBASE,HIVE ...

  6. [转]glew, glee与 gl glu glut glx glext的区别和关系

    原文地址:http://blog.csdn.net/delacroix_xu/article/details/5881942 因为也是初接触,所以就当了解,等深入学习后再回顾这篇文章观点. GLEW是 ...

  7. 安装Oracle 11g RAC R2 之Linux DNS 配置

    Oracle 11g RAC 集群中引入了SCAN(Single Client Access Name)的概念,也就是指集群的单客户端访问名称.SCAN 这个特性为客户端提供了单一的主机名,用于访问集 ...

  8. s:iterator标签的使用

    1.在说明s:iterator标签的使用前,先了解下struts2中的Value Stack. 这里参考了webwork中对Value Stack的描述,由于struts2是在webwork的基础上进 ...

  9. socket 基础知识

    PHP使用Berkley的socket库来创建它的连接.socket只不过是一个数据结构.你使用这个socket数据结构去开始一个客户端和服务器之间的会话.这个服务器是一直在监听准备产生一个新的会话. ...

  10. Bootstrap学习笔记上(带源码)

    阅读目录 排版 表单 网格系统 菜单.按钮 做好笔记方便日后查阅o(╯□╰)o bootstrap简介: ☑  简单灵活可用于架构流行的用户界面和交互接口的html.css.javascript工具集 ...