我每次写信都应该刷新ServletOutputStream吗?

时间:2016-02-26 17:12:54

标签: java jsp exception outputstream

我在AttachedFiles.jsp页面中收到以下错误:

SEVERE: Servlet.service() for servlet [jsp] in context with path [/da8] threw exception [java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:678)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:213)
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:194)
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126)
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80)
    at org.apache.jsp.jsp.AttachedFiles_jsp._jspService(AttachedFiles_jsp.java:786)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)

我以前的版本有:

fileIn.close();
outStream.flush();
outStream.close();
在while循环之后

,但我认为在finally块中关闭所有对象是一个好习惯。 我还读到close()也刷新了OutputStream。

在我做出这个改变后,我遇到了上述错误:/

AttachedFiles.jsp

    FileInputStream fileIn = null;
    ServletOutputStream outStream = null;
    try 
    {   
        File fileToDownload = new File(file);

        response.setContentType("APPLICATION/OCTET-STREAM");
        String fileName = request.getParameter("fileName");
        if(fileName == null) fileName = "";

        String disHeader = "Attachment; Filename=\""+fileName+"\"";
        response.setHeader("Content-Disposition", disHeader);
        fileIn = new FileInputStream(fileToDownload);
        outStream = response.getOutputStream();

        byte[] outputByte = new byte[(int) fileToDownload.length()];

        //copy binary contect to output stream
        while(fileIn.read(outputByte) != -1)
        {
            outStream.write(outputByte);
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            outStream.close();
            if (fileIn != null)
                fileIn.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

有谁知道我收到此错误的原因?

如果是这样,我应该在写入outputStream后立即在while循环中刷新吗?

1 个答案:

答案 0 :(得分:0)

JSP编译成Servlet。它已经在生成的Servlet中准备了它的隐式对象:

  • request
  • 回复
  • out(PrintWriter)
  • session
  • 申请
  • 配置
  • pageContext

你可以用来写。但是出来是一个作家,所以它写的字符。您可能想要写二进制文件。

如果将代码重写为 Servlet ,那将会很酷。它会更清洁。

否则,要解决一个JSP内部问题,可以说这里的内容很糟糕......不会很酷。

  • 你可以插入一个明确的" return;"写完之后。
  • 您可以尝试清除缓冲区并重置内部状态。
  • 在jsp
  • 附近添加疯狂捕获
  • 至少,不要太苛刻,添加<%@ page trimDirectiveWhitespaces =" true" %GT;确保不会为页面创建JspWriter。