今天写java文件下载程序,完成后使用一切正常,但是总抛出java.lang.IllegalStateException异常,虽然并不影响正常使用,但看了总让人觉得很不舒服,检查代码并没有错,最后上网查了不少资料,终于发现原因之所在。
我们在做文件上传或者下载,或者过滤等操作时,可能要用到页面的输出流.
例如在action中使用:
response.reset();
response.setContentType(”application/vnd.ms-excel”);
OutputStream os = response.getOutputStream();
抛出异常:java.lang.IllegalStateException
原因分析:
这是web容器生成的servlet代码中有out.write(””),这个和JSP中调用的response.getOutputStream()产生冲突.
即Servlet规范说明,不能既调用 response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出 IllegalStateException,
因为在jsp中,out变量是通过response.getWriter得到的,在程序中既用了response.getOutputStream,又用了out变量,故出现以上错误。
法一:在JSP文件中,加入下面两句
<% out.clear(); out = pageContext.pushBody(); %>
|
此法的缺陷:
很多开发项目并不是JSP前端,如freemarker,velocity等
造成问题的"response.getOutputStream()"并未被写在JSP里,而是写在servlet/action里
法二: 在action中,不要return 回具体的result文件,而是return null
//return SUCCESS; return null;
|
分享到:
相关推荐
纠结了半天的 java.lang.IllegalStateException: getOutputStream() has already。这个问题困扰了半天,在网上查阅了大量资料 出这个错误一般就是下面2个.....
NULL 博文链接:https://hw1287789687.iteye.com/blog/1925191
1. java.lang.IllegalStateException: No wrapped connection. 2.java.lang.IllegalStateException: Adapter is detached. 原因: 1.单线程一次执行一个请求可以正常执行,如果使用多线程,同时执行多个请求时就会...
异常:Caused by: java.lang.IllegalStateException: Method has too many Body parameters Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract ...
NULL 博文链接:https://anminer.iteye.com/blog/845067
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but...
weblogic12c部署webservice错误问题WSSERVLET11: 无法解析运行时描述符: java.lang.IllegalStateException: MASM0001:
Type 异常报告 消息 Failed to convert ... nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Date': no matching editors or co
解决java.lang.IllegalStateException: unread block data的架包
java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader....
spring 总的概述。。。入门好教程~~~~~~~~~~~~~
各种数据库在JDBC中的连接方法.及在struts与hibernate中的配置..
今天公司springboot项目准备部署到...java.lang.IllegalStateException: 启动子级时出错 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720) at org.apache.catalina.core.Con
2013-08-12 14:33:37.672:... Add CGLIB to your classpath.:java.lang.IncompatibleClassChangeError: class net.sf.cglib.core.DebuggingClassWriter has interface org.objectweb.asm.ClassVisitor as super class
因工作的缘故,有机会接触了Java本地方法调用...使用Java的JNI调用C/C++的动态联结库有个固定的步骤,下面将以一个最简单的HelloWorld例子程序来说明调用过程。这个HelloWorld的例子只是简单的显示HelloWorld消息。
里面包含了java通过xfire访问webservice用到的jar包,已经打包好了
这个存储库提供了一种在处理片段传输和后台任务时避免“java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState”的方法。 您可以在的非常权威的阅读有关该问题和可能的解决方案...
主要介绍了java 实现websocket的两种方式实例详解,一种使用tomcat的websocket实现,一种使用spring的websocket,本文通过代码给大家介绍的非常详细,需要的朋友可以参考下
oop object c, for apple ios developer