Vig*_*ino 5 java jsp servlets outputstream illegalstateexception
当用户点击按钮时,客户端浏览器上的windchill GUI应该在他的系统上下载特定的pdf文件.我已经通过使用以下代码实现了这一点.
<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>
Run Code Online (Sandbox Code Playgroud)
此代码工作正常,文件正在下载但之后它会引发异常.以下是我的方法服务器日志
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: ......
Run Code Online (Sandbox Code Playgroud)
......
谷歌有很多关于这个例外的帖子,但我无法清除我的错误.我也试过在servlet而不是jsp中添加它.这也显示了相同的异常.这样下载文件的方式是正确的还是我错了?我需要帮助
谢谢
Ram*_*PVK 14
你不能同时使用getServletOutputStream(),并getWriter()在相同的反应.
来你的问题.避免在JSP中编写scriptlet.无论你在JSP中做什么,都要在Servlet中实现它.
你response.getOutputStream();在JSP 中调用是非法的.你应该使用ServletResponse.getOutputStream()或ServletResponse.getWriter().由于JSP默认使用ServletResponse.getWriter().你应该写ServletResponse.getWriter()而不是ServletResponse.getOutputStream()
这就是Java Doc所说的:
的getOutputStream ...
ServletOutputStream getOutputStream()抛出IOException
返回适合在响应中写入二进制数据的ServletOutputStream.servlet容器不对二进制数据进行编码.
在ServletOutputStream上调用flush()会提交响应.可以调用此方法或getWriter()来编写正文,而不是两者.
返回:用于写入二进制数据的ServletOutputStream 抛出:IllegalStateException - 如果已在此响应上调用getWriter方法