我正在使用此代碼從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後,也關閉輸出流但沒有奏效。請協助。
是你函數中的完整代碼嗎? –