java. Lang. IllegalStateException: getoutputstream() has been called for this response
See English answers > getoutputstream() has already been called for this response 12
So I created a servlet and got the parameters in the URL from the query string
According to the parameter properties, several web service related methods are called to obtain the file details and file contents Now invoke the file transfer between the servlet and the user system
The biggest concern is that the code snippet works properly due to the following exceptions The user can save the document in the desired location I tried to figure out why I received this error
Error:
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,length); } in.close(); outStream.flush(); } catch (Exception e) { e.printStackTrace(); }finally{ outStream.close(); } }
Solution
When working with the struts 2 framework, you encounter the same problem when using web applications
In my example, the action class of downloading a file returns a string "sucess" to the request after the file is downloaded, which leads to the problem I changed the return type of the method to void and solved the problem