Java.lang.IllegalStateException: output stream already in use

windchill GUI on a client-side browser, when a user clicks on a button, a specific pdf file must be downloaded to his system. I achieved this using the following code.

<body> <% String pdfname= session.getAttribute("pdfname").toString(); String Pdfpath= session.getAttribute("pdfpath").toString(); File f =new File(Pdfpath); Boolean flag=false; if(f.exists()) { BufferedInputStream filein = null; BufferedOutputStream out2=null; try { File file = new File(Pdfpath);//specify the file path byte b[] = new byte[1048576]; int len = 0; filein = new BufferedInputStream(new FileInputStream(file)); out2=new BufferedOutputStream(response.getOutputStream()); response.setHeader("Content-Length", ""+file.length()); response.setContentType("application/pdf"); response.setHeader("Content-Disposition","attachment;filename="+pdfname); response.setHeader("Content-Transfer-Encoding", "binary"); while ((len = filein.read(b)) > 0) { out2.write(b, 0, len); out.println("Your Pdf Document Is Generated Please close it"); } filein.close(); out2.flush(); out2.close(); } catch(Exception e) { out.println(e); } }else{ String error ="File Not Found Or File Has Bean Deleted Already"; request.setAttribute("error", error); RequestDispatcher s = request.getRequestDispatcher("NoFile.jsp"); s.forward(request, response); } %> </body> 

This code works fine and the file loads, but after that it throws an exception. The following is the log of my method log

 ERROR : org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/Windchill].[jsp] - Servlet.service() for servlet jsp threw exception Thu 3/28/13 12:29:07: TP-Processor7: java.lang.IllegalStateException: Already using output stream Thu 3/28/13 12:29:07: TP-Processor7: at wt.servlet.CompressionFilter$GzippingResponse.getWriter(CompressionFilter.java:860) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:173) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jasper.runtime.JspWriterImpl.close(JspWriterImpl.java:187) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jsp.netmarkets.jsp.gt.get_jsp._jspService(get_jsp.java:105) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) Thu 3/28/13 12:29:07: TP-Processor7: at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) Thu 3/28/13 12:29:07: TP-Processor7: at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374) Thu 3/28/13 12:29:07: TP-Processor7: at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302) Thu 3/28/13 12:29:07: TP-Processor7: ...... 

......

There are many posts on google about this exception, but I cannot fix my error. I also tried adding this to the servlet instead of jsp. This also shows the same exception. Is this the right way to upload a file, or am I mistaken? I need help

thanks

+4
source share
3 answers

You cannot use both getServletOutputStream() and getWriter() in the same answer.

Come to your problem. Avoid writing scripts in JSP. Whatever you do in JSP, execute it in Servlet.

You call response.getOutputStream(); in JSP, which is illegal. You must use either ServletResponse.getOutputStream() or ServletResponse.getWriter() . Because JSP uses ServletResponse.getWriter () by default. You should write ServletResponse.getWriter () instead of ServletResponse.getOutputStream ()

This is what the Java Doc says:

getOutputStream ...

ServletOutputStream getOutputStream () throws an IOException

Returns a ServletOutputStream suitable for writing binary data in response. The servlet container does not encode binary data.

A call to flush () in ServletOutputStream completes the response. Either this method or getWriter () can be called to write the body, and not both.

Returns: ServletOutputStream for writing binary data Throws: IllegalStateException - if the getWriter method was called for this answer

+12
source

It seems that the exception is selected from this line.

 out.println(e); 

In the event that your code for sending a PDF as an application fails during recording, it will throw an exception, and when you try to print an exception with the above outputputstream line is already used.

Do not mix user interface and business logic in JSP. Use the servlet to complete this task.

+1
source

You have </body> after your script. It will print something in the response flow of the response, but you have already closed it before.

In what sense should it include <body> tags in your answer when what you really want to do is pass the PDF back to the client?

0
source

All Articles