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
source share