JSP:已经为此响应调用了getOutputStream(). [英] JSP : getOutputStream() has already been called for this response

查看:125
本文介绍了JSP:已经为此响应调用了getOutputStream().的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从FTP下载文件. 如下所示,我在tomcat日志中遇到异常,我可以执行任务,但是日志大小增加了很多.

I am using this code to download file from FTP. I am getting exception in tomcat log as below, i am able to perform my task but my log size increases a lot.

代码:

   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的帖子,也关闭了输出流,但是没有一个起作用.请协助.

I saw post regarding adding code to servlet, also closing the output stream but none of that worked. Please assist.

推荐答案

您不应该在JSP中执行此操作,但应该使用Servlet(即使jsp页面更为实用)

you shouldn't do this in JSP but you should use a Servlet (even if a jsp page is far more pratical)

但是,如果仍然要使用jsp页面,请使用以下指令:

however if you still want to use a jsp page, use this directive:

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

除了问题之外,因为您使用的是commons-io:

apart from question, since you are using 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");}

这篇关于JSP:已经为此响应调用了getOutputStream().的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆