JSP:已经为此响应调用了getOutputStream()

时间:2014-01-18 11:27:08

标签: java jsp servlets

我正在使用此代码从FTP下载文件。 我在tomcat日志中得到异常,如下所示,我能够执行我的任务,但我的日志大小增加了很多。

代码:

   if (file.exists())
   {
       if (file.canRead())
       {
          // IE6 & SSL PDF Bug
          // http://forums.sun.com/thread.jspa?threadID=526451&start=15&tstart=0

           mimeType = new javax.activation.MimetypesFileTypeMap().getContentType(file);
           response.setHeader("Cache-Control","private");
           response.setHeader("Pragma","expires");
           response.setHeader("Content-Disposition", "inline; filename=\"" + org.apache.commons.io.FilenameUtils.getName(file.getAbsolutePath()) + "\"");
           response.setContentType(mimeType);
           response.setContentLength((new Long(file.length())).intValue());
           byte[] buffer = new byte[(int)org.apache.commons.io.FileUtils.ONE_KB * 64];
           output=response.getOutputStream();
           bos = new java.io.BufferedOutputStream(output, buffer.length);
           bis = new java.io.BufferedInputStream(new java.io.FileInputStream(file));
           while (bis.read(buffer) != -1)
           {
               bos.write(buffer);
           }
           bos.flush();
       }
       else{System.out.println("Cannot read from file");}
   }
   else{System.out.println("File dosen't exist");}

错误消息

    Jan 18, 2014 6:11:31 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception
 java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:611)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
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:188)
at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)

我看到有关向servlet添加代码的帖子,也关闭了输出流,但没有一个工作。请协助。

2 个答案:

答案 0 :(得分:3)

你不应该在JSP中这样做但你应该使用Servlet(即使jsp页面更加实用)

但是,如果您仍想使用jsp页面,请使用此指令:

<%@page language="java" trimDirectiveWhitespaces="true"%>

除了问题,因为你正在使用commons-io:

if (file.exists())
{
    if (file.canRead())
    {
       // IE6 & SSL PDF Bug
       // http://forums.sun.com/thread.jspa?threadID=526451&start=15&tstart=0

        mimeType = new javax.activation.MimetypesFileTypeMap().getContentType(file);
        response.setHeader("Cache-Control","private");
        response.setHeader("Pragma","expires");
        response.setHeader("Content-Disposition", "inline; filename=\"" + org.apache.commons.io.FilenameUtils.getName(file.getAbsolutePath()) + "\"");
        response.setContentType(mimeType);
        response.setHeader("Content-Length", String.valueOf(file.length()));

        OutputStream output = response.getOutputStream();
        FileUtils.copyFile(file, output);
        output.close();
    }
    else{System.out.println("Cannot read from file");}
}
else{System.out.println("File dosen't exist");}

答案 1 :(得分:0)

使用Servlet在响应中发送二进制数据,而不是JSP页面。 JSP页面发送文本输出,并将调用getWriter方法以便从JSP页面发送缓冲输出。

另见this question.