windchill GUI on client side browser when a user clicks on a button particular pdf file should gets downloaded on his system.I have achieved this by 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 file is getting downloaded but after that It throws an exception.The following is my method server 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 lots of post available in google regarding this exception but I can't clear my mistake. I've also tried adding this in servlet instead of jsp. That too shows the same exception.Is this way correct for downloading a file or I'm in wrong way? I need help
Thanks