title: struts2 - java.lang.IllegalStateException occurs intermittently while action calls result stream to download PDF file
body: <p>I have an action called GetFile which directly opens a file download box to open PDF.</p>
  
  <p>Below is the struts.xml configuration and the action class for the same. I am using result type as stream to achieve this. I can see that the pdf download box opens all the times and i am also able to download the file. </p>
  
  <p>But I find that there were some requests last night that had fired the action and it has produced the below error. </p>
  
  <pre><code>    org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
          at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:570)
          at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:452)
          at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
          at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
          at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
          at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
          at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
          at org.apache.struts2.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:159)
          at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
          at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:374)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:278)
          at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
          at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
          at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
          at deshaw.irweb.web.interceptor.AuthInterceptor.intercept(AuthInterceptor.java:60)
          at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
          at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
          at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:510)
          at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
          at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
          at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:294)
          at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:183)
          at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:169)
          at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
          at java.lang.Thread.run(Thread.java:722)
  Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response
          at org.apache.catalina.connector.Response.getWriter(Response.java:636)
          at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:205)
          at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
          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:182)
          at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:123)
          at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
          at org.apache.jsp.jsp.ServerError_jsp._jspService(ServerError_jsp.java:157)
          at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
          at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
          ... 38 more     
  </code></pre>
  
  <p>Can someone tell me what could have caused the problem ? I am not able to reproduce the problem currently. I read somewhere that it could be due to closing the download dialog, but it was not the case even after i tried from multiple browsers. Also checked <a href="https://stackoverflow.com/questions/1776142/getoutputstream-has-already-been-called-for-this-response">getOutputStream() has already been called for this response</a> , but could not relate directly to my problem as there is no redirection to jsp as the struts2 action directly leads to result stream.</p>
  
  <h1>struts.xml</h1>
  
  <pre><code>&lt;action name="GetFile" class="Class" method="DownloadFile"&gt;
              &lt;result name="success" type="stream"&gt;
              &lt;param name="contentType"&gt;contentType&lt;/param&gt;
              &lt;param name="contentDisposition"&gt;fileName&lt;/param&gt;
              &lt;param name="bufferSize"&gt;1024&lt;/param&gt;
              &lt;param name="inputName"&gt;inputStream&lt;/param&gt; 
              &lt;/result&gt;
  &lt;/action&gt;
  </code></pre>
  
  <h1>Action Class</h1>
  
  <pre><code>public String DownloadFile ()
  {
          // create the object variable pdfdoc which is a custom object.
          // You can assume that the pdfdoc is properly constructed 
          // and getFileContent does what it is required to do, so
          // that the final pdf is generated.
          ...
          ...
          inputStream = new ByteArrayInputStream(pdfdoc.getFileContent());
          contentType = pdfdoc.getContentType();
          fileName = pdfdoc.getFileName();
          contentDisposition = doc.getContentDisposition();
          bufferSize = 1024;
          return "success";
  
  }
  </code></pre>