Struts2 - java.lang.IllegalStateException occurs intermittently, while the action causes a stream of results to load the PDF file

I have an action called GetFile that directly opens a file download window to open a PDF.

Below is the struts.xml configuration and action class for the same. To achieve this, I use the result type as a stream. I see the pdf download window open all the time, and I can also download the file.

But I found that yesterday there were some requests that dismissed the action, and this led to an error below.

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 

Can someone tell me what might cause the problem? I can not reproduce the problem at the moment. I read somewhere that this could be due to closing the download dialog, but it wasn’t, even after I tried from several browsers. Also checked getOutputStream () was already called for this answer , but could not directly relate my problem, since there is no redirect to jsp, since the action of struts2 directly leads to the result of the stream.

struts.xml

 <action name="GetFile" class="Class" method="DownloadFile"> <result name="success" type="stream"> <param name="contentType">contentType</param> <param name="contentDisposition">fileName</param> <param name="bufferSize">1024</param> <param name="inputName">inputStream</param> </result> </action> 

Action class

 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"; } 
+6
source share
1 answer

I had a similar problem when using Internet Explorer and fixed it by adding this to the response header:

 response.setHeader("Expires","0"); response.setHeader("Pragma","cache"); response.setHeader("Cache-Control","private"); 

Perhaps the queries you see in your log are generated by Internet Explorer.

Look here and here !

Update. To print the browser version in your log, you can do something like this:

 System.out.println(request.getHeader("User-Agent")); 
+1
source

Source: https://habr.com/ru/post/917189/


All Articles