Java.lang.IllegalStateException: getOutputStream () has already been called for this response

When a user clicks on a specific link, use Web Services to transfer the document from the Remote ECM system to the user's computer.

So, I created a servlet and query string and get parameters from the url.

Depending on the parameter attributes, several web service-related methods were used to retrieve file information and file content. now invoke file transfers between servlets and the user system.

The biggest concern is that with the exceptions below, the code snippet works fine. the user can save the document to the desired location. I was trying to figure out why I am getting this error.

Errors:

java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:611)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
t  DownloadServlet.doGet(DownloadServlet.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

      

Code ## DownloadServlet.java

 getDocument(HttpServletRequest request,HttpServletResponse response)
   {
 \\used Custom web services methods to get filename with extensions from external ECM system 
   File resultFile = content.getAsFile();
   response.setContentType("application/octet-stream");  
   ServletOutputStream outStream = response.getOutputStream();          
   try {
       response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
       byte[] byteBuffer = new byte[BUFSIZE];
       DataInputStream in = new DataInputStream(new FileInputStream(file));
          while ((in != null) && ((length = in.read(byteBuffer)) != -1)){
            outStream.write(byteBuffer,0,length);
            }
            in.close();
            outStream.flush();                              
      } catch (Exception e) {
          e.printStackTrace();
      }finally{
          outStream.close();
      }

 }

      

+3


source to share


2 answers


I had the same problem while running a web application using the Struts 2 framework work. I searched for a while and the solution I found worked for me. I found somewhere that:

The basic rule of HTTP is one request, one response. You can only send one message per request. Either an HTML page, a PDF document, or an image, etc. Java complains if you have already received a write / output stream as you should only receive one of them.



In my case, the action class that uploads the file returns a "sucess" request to the request after uploading the file, which caused this issue. I changed the return type of the method to void and resolved the problem.

+6


source


You have a problem with the answer.
So use this two lines.



 response.getOutputStream().flush();
 response.getOutputStream().close();

      

+1


source







All Articles