Java JSP:已经为此响应调用了 getOutputStream()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21203450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSP : getOutputStream() has already been called for this response
提问by Pramit Parikh
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.
我正在使用此代码从 FTP 下载文件。我在 tomcat 日志中遇到异常,如下所示,我能够执行我的任务,但我的日志大小增加了很多。
Code:
代码:
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");}
Error Message
错误信息
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)
I saw post regarding adding code to servlet, also closing the output stream but none of that worked. Please assist.
我看到了关于向 servlet 添加代码的帖子,也关闭了输出流,但没有一个工作。请协助。
回答by Erwin Bolwidt
Use a Servlet to send binary data in a response, not a JSP page. JSP pages send text output, and will invoke the getWriter method in order to send the buffered output from the JSP page.
使用 Servlet 在响应中发送二进制数据,而不是 JSP 页面。JSP 页面发送文本输出,并将调用 getWriter 方法以从 JSP 页面发送缓冲输出。
See also this question.
另见这个问题。
回答by Michele Mariotti
you shouldn't do this in JSP but you should use a Servlet (even if a jsp page is far more pratical)
您不应该在 JSP 中执行此操作,但您应该使用 Servlet(即使 jsp 页面更实用)
however if you still want to use a jsp page, use this directive:
但是,如果您仍想使用 jsp 页面,请使用以下指令:
<%@page language="java" trimDirectiveWhitespaces="true"%>
apart from question, since you are using commons-io:
除了问题,因为您使用的是 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");}