java.lang.IllegalStateException: getOutputStream() has already been ca...
2011-09-10 20:45
435 查看
tomcat正常启动。
但是一出来有验证码的页面,后台就会报错:
信息: Server startup in 13157 ms
2008-01-09 21:35:40,390 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/mi].[jsp]] - Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:601)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:185)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImp
在网上查了就是getOutputStream()方法和getWriter()方法只能用一个。但是我没有找到在哪里又调用了getWriter()方法。也不知道怎么改动,这么老是报错心里可不舒服。
最佳答案
out.clear();
out = pageContext.pushBody();
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
产生这样的异常原因:是web容器生成的servlet代码中有out.write(""),这个和JSP中调用的response.getOutputStream()产生冲突.即Servlet规范说明,不能既调用response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出IllegalStateException,因为在jsp中,out变量实际上是通过response.getWriter得到的,你的程序中既用了response.getOutputStream,又用了out变量,故出现以上错误。
但是一出来有验证码的页面,后台就会报错:
信息: Server startup in 13157 ms
2008-01-09 21:35:40,390 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/mi].[jsp]] - Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:601)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:196)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:185)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:116)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImp
在网上查了就是getOutputStream()方法和getWriter()方法只能用一个。但是我没有找到在哪里又调用了getWriter()方法。也不知道怎么改动,这么老是报错心里可不舒服。
最佳答案
具体的原因就是 在tomcat中jsp编译成servlet之后在函数_jspService(HttpServletRequest request, HttpServletResponse response)的最后 有一段这样的代码 finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } 这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和 response.getOutputStream()相冲突的!所以会出现以上这个异常。 然后当然是要提出解决的办法: 在使用完输出流以后调用以下两行代码即可: out.clear(); out = pageContext.pushBody(); 文件代码如下: OutputStream o=response.getOutputStream(); //输出文件用的字节数组,每次发送500个字节到输出流 byte b[]=new byte[500]; //下载的文件 File fileLoad=new File("f:/2000","book.zip"); //客户使用的保存文件的对话框 response.setHeader("Content-disposition","attachment;filename="+"book.zip"); //通知客户文件的MOME类型 response.setContentType("application/x-tar"); //通知客户文件的长度 long fileLength=fileLoad.length(); String length=String.valueOf(fileLength); response.setHeader("Content_Length",length); out.clear(); out = pageContext.pushBody(); //读取文件book.zip,并发送给客户下载 FileInputStream in=new FileInputStream(fileLoad); int n=0; while((n=in.read(b))!=-1) { o.write(b,0,n); } |
out = pageContext.pushBody();
// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
产生这样的异常原因:是web容器生成的servlet代码中有out.write(""),这个和JSP中调用的response.getOutputStream()产生冲突.即Servlet规范说明,不能既调用response.getOutputStream(),又调用response.getWriter(),无论先调用哪一个,在调用第二个时候应会抛出IllegalStateException,因为在jsp中,out变量实际上是通过response.getWriter得到的,你的程序中既用了response.getOutputStream,又用了out变量,故出现以上错误。
相关文章推荐
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- Java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response的解决办法
- java.lang.IllegalStateException: getOutputStream() has already been called for this response解决方法及解释
- java.lang.IllegalStateException:getOutputStream() has already been called for this response的解决方法
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response 分类: 异常
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called f
- java.lang.IllegalStateException: getOutputStream() has already been called f
- 严重: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: getOutputStream() has already been called
- java.lang.IllegalStateException: getOutputStream() has already been called for this response,java.la
- java.lang.IllegalStateException: getOutputStream() has already been called 解决办法