2014-01-18 72 views
2

我正在使用此代碼從FTP下載文件。 我在下面的tomcat日誌中遇到異常,我能夠執行我的任務,但我的日誌大小增加了很多。JSP:getOutputStream()已被調用此響應

代碼:

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後,也關閉輸出流但沒有奏效。請協助。

+0

是你函數中的完整代碼嗎? –

回答

0

使用Servlet在響應中發送二進制數據,而不是在JSP頁面中發送。 JSP頁面發送文本輸出,並將調用getWriter方法以便從JSP頁面發送緩衝輸出。

又見this question.

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");} 
相關問題