java.lang.IllegalStateException : Already using output stream 错误

标签 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>

这段代码工作正常,文件正在下载,但之后它抛出异常。以下是我的方法服务器日志

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: ......

……

Google 中有很多关于此异常的帖子,但我无法清除我的错误。我也试过在 servlet 而不是 jsp 中添加它。这也显示了同样的异常。这种下载文件的方式是否正确,或者我的方式有误?我需要帮助

谢谢

最佳答案

您不能在同一个响应中同时使用 getServletOutputStream()getWriter()

来解决你的问题。避免在 JSP 中编写 scriptlet。无论您在 JSP 中做什么,都在 Servlet 中实现。

您正在 JSP 中调用 response.getOutputStream();,这是非法的。您应该使用 ServletResponse.getOutputStream()ServletResponse.getWriter()。 由于 JSP 默认使用 ServletResponse.getWriter()。您应该写入 ServletResponse.getWriter() 而不是 ServletResponse.getOutputStream()

Java 文档是这样说的:

getOutputStream...

ServletOutputStream getOutputStream() throws IOException

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

Calling flush() on the ServletOutputStream commits the response. Either this method or getWriter() may be called to write the body, not both.

Returns: a ServletOutputStream for writing binary data Throws: IllegalStateException - if the getWriter method has been called on this response

关于java.lang.IllegalStateException : Already using output stream 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15676337/

相关文章:

java - 顺序搜索 Java

java - Android 找不到类 java.awt.datatransfer.StringSelection?尝试将字符串放在剪贴板上

eclipse - JSTL 问题,找不到 jasperException uri

java - 如何在 Web 服务 Java EE 中使用 EJB

java - 多对一关系的复合主键不支持映射报错

java - 从 java 更新 Blob 时出现 SQL 语法错误

java - Android 状态栏保持不变

java - 在Struts2中上传多个文件并维护索引(文件上传的位置)

java - jsp中的表单,用什么代替隐藏字段

java - 提交响应后无法调用 sendRedirect()