就当上一个按钮,特别是PDF文件,用户必须点击他system.I会下载客户端浏览器GUI风速:通过已经使用下面的代码来实现这一点。
<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>
此代码工作正常,文件越来越下载,但之后,它会引发以下的exception.The是我的方法服务器日志
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: ......
......
有很多岗位提供谷歌对此异常,但我不能清除我的错误。 我也试过在servlet的,而不是JSP添加此。 这也显示了相同的exception.Is这样正确的下载文件或我在错误的方式? 我需要帮助
谢谢