getOutputStream() has already been called for this response

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010297957/article/details/42914519

刚刚研究项目的时候,遇到这个错误。搜了很多资料并自己研究了好久终于算是基本解决了这个问题,在这里记录一下。

出现错误的原因,说出来很简单:

     一般都是在JSP中调用了:response.getOutputStream();来获取输出流。并且有意或者无意的使用了out对象来输出内容。

 

异常的显式原因(自身代码原因):

1、我们都知道,JSP最终会被翻译为一个Java类(Servlet),然后在页面中的所有HTML内容,都是在这个Servlet中的_jspService()方法里面被输出的。

输出的方式是out.write("****");

 

2、又因为JSP页面中你所有写的不加"<%%>"这两个括号的内容都会被自动翻译为out.write("****");

    1、不加"<%%>"是包括"<%%>"、"<%@%>"、"<%!%>"的内容。

    2、out是JspWriter,字符输出流。输出的内容包括全部的HTML,以及你无意中会写的回车+换行

 

3、问题出现了:如果你调用了response.getOutputStream()来获取输出流,又无意中out输出了("\r\n")(当然,你自己显示调用out更不行了)就会出现错误。

错误就是getOutputStream() has already been called for this response。

 

异常的真实原因(java设计使然):

jsp中的out对象,在jsp中是通过这句代码获得的:pageContext.getOut();

    1、经过我研究发现:如果你并没有在JSP中写需要write的内容(write方法没有被掉用过),似乎out对象根本就没有被创建。

    2、一旦JSP中使用了out.write(),那么pageContext.getOut()就真正的返回了一个JspWriter对象。

    3、这样就不知道在哪最终调用了ServletResponse.getWriter();

    4、又因为你之前调用了response.getOutputStream();

    5、这样最终触发了3和4方法中相同的异常。

        getWriter():IllegalStateException - if the getOutputStream method has already been called for this response object

        getOutputStream():IllegalStateException - if the getOutputStream method has already been called for this response object

        这两个方法是不能共存的,在他们的方法定义中也有描述:

        Either this method or getOutputStream() may be called to write the body, not both.
   Either this method or getWriter() may be called to write the body, not both.

 

解决方法:

1、无意中使用了out对象的情况:

仔细查找页面中的代码,删除%><%之间的所有内容包括空格和换行符。不能出现所有非Java代码的内容。

 

2、确实需要使用out对象也要使用getOutputStream的情况:

在JSP页面Java代码块的最后加上:out.clear();

 

阅读更多
换一批

错误信息:getOutputStream() has already been called for this response

11-22

在写下载(把数据保存在txt中打开)时出现的提示。麻烦大家帮忙想招啦,应该是遇到response.getOutputStream()这句的时候报的错,但我是清空再做的操作啊,我自己实在是找不到解决办法。我的代码如下:rnrnrnif (type.equals("link4_one_loaddown")) rn File f = null;rn FileInputStream fileinputstream = null;rn String downloadfilepath = "/WEB-INF/download/";rn String filename = "testDownload.txt";rn String path= request.getRealPath("/");rn String filepathname = path + downloadfilepath;rnrnrn //写入文件完成rn EmployeeBean empbean = new EmployeeBean(ds);rn EmployeeVO emp = empbean.getEmployee(request.getParameter("id"));rn //读出文件开始rn try rn f = new File(filepathname, filename);rn f.createNewFile();//创建文件rn LoadDownBean loadbean=new LoadDownBean();rn loadbean.writeFile(emp,f); //写文件的具体过程rnrnrn fileinputstream = new FileInputStream(rn filepathname+ filename);rnrn servletoutputstream = response.getOutputStream();rn response.reset();rn response.setHeader("Content-disposition",rn "attachment; filename=" + filename);rn response.setContentType("text/plain;charset=gb2312");rn int j = 0;rn int k = 0;rn byte abyte0[] = new byte[1024];rn while ((k = fileinputstream.read(abyte0)) != -1) rn j += k;rn servletoutputstream.write(abyte0);rn rnrn catch (Exception ex) rn ex.printStackTrace();rn finally rn try rn servletoutputstream.close();rn fileinputstream.close();rn File f1 = new File(filepathname+filename);rn if (f1.exists()) rn System.out.println("linnan");rn f1.delete(); //删除File.txt文件rn rn catch (Exception ex1) rn ex1.printStackTrace();rn rn rnrn报错信息如下:rnERROR - Servlet.service() for servlet action threw exceptionrnjava.lang.IllegalStateException: getOutputStream() has already been called for this responsern at org.apache.catalina.connector.Response.getWriter(Response.java:596)rn at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:186)rn at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:156)rn……

getOutputStream() has already been called for this response 问题

11-25

做一个jsp下载excel的功能,功能描述如下:首先将excel下载到服务器,然后从服务器下载到本地。 rn最开始是一个页面,点击下载,用ajax跳转到后台,将文件下载到服务器,然后跳回jsp,执行如下js: rnif(xmlHttpRequest.status==200)//服务器状态码 rn var loadDir = "/webrpt/loadFile.jsp?flag=1&dir=BBSJ_DOWNLOAD.zip" ; rn window.location.href (loadDir) ; rn rn此时跳转到下载jsp页面,自动弹出下载窗口,下载jsp loadFile.JSP代码如下: rn[code=Java]rn<%@ page contentType="text/html;charset=GBK" %> rn <%@ page import="java.io.*" %> rn rn rn rn <% rn java.io.BufferedInputStream bis=null; rn java.io.BufferedOutputStream bos=null; rn try rn String filename=request.getParameter("dir"); rn response.reset(); rn File fileLoad=new File("D://",request.getParameter("dir")); rn filename=new String(filename.getBytes("iso8859-1"),"gbk"); rn response.setContentType("application/x-download"); rn response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("gbk"),"iso8859-1")); rn bis=new java.io.BufferedInputStream(new java.io.FileInputStream(fileLoad)); rn bos=new java.io.BufferedOutputStream(response.getOutputStream()); rn byte[] buff = new byte[2048]; rn int bytesRead; rn while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) rn bos.write(buff,0,bytesRead); rn rn bos.flush(); rn catch(Exception e) rn e.printStackTrace(); rn finally rn if(bis != null)bis.close(); rn if(bos != null)bos.close(); rn %> rn rn rn[/code]rn加断点显示,当弹出下载窗口的时候还没报错,再执行之后的outputstream出错,错误信息如下: rn严重: Servlet.service() for servlet debugjsp threw exception rnjava.lang.IllegalStateException: getOutputStream() has already been called for this response rnat org.apache.catalina.connector.Response.getWriter(Response.java:596) rnat org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:186) rnat org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124) rnat org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:117) rnat org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:191) rnat org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115) rnat org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75) rnat org.apache.jsp.loadFile_jsp._jspService(org.apache.jsp.loadFile_jsp:84) rnat org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97) rnat javax.servlet.http.HttpServlet.service(HttpServlet.java:802) rnat org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322) rnat org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291) rnat org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241) rnat javax.servlet.http.HttpServlet.service(HttpServlet.java:802) rnat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252) rnat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173) rnat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) rnat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) rnat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) rnat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) rnat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) rnat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) rnat org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) rnat org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744) rnat org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) rnat org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) rnat org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) rnat java.lang.Thread.run(Thread.java:595) rnrn整了一天了,一直没有解决。。。。rn

没有更多推荐了,返回首页