[英]best practice response.getOutputStream
any comments on my code on allowing user to download file. 允许用户下载文件的任何关于我的代码的评论。
if(fileObject !=null)
response.setHeader("Content-disposition", "attachment; filename=\""+fileObject.getFilename()+"\"");
response.setContentType(fileObject.getFiletype());
response.setContentLength((int)fileObject.getFilesize().intValue());
try {
if(response !=null && response.getOutputStream() !=null &&fileObject!=null && fileObject.getBinData() !=null ){
OutputStream out = response.getOutputStream();
out.write(fileObject.getBinData());
}
} catch (IOException e) {
throw new ApplicationRuntimeException(e);
}
most of the time, i dont get below error. 大多数时候,我不会低于错误。 but once and a while, i get error 但有一段时间,我得到错误
29 Nov 2010 10:50:41,925 WARN [http-2020-2] - Unable to present exception page: getOutputStream() has already been called for this response
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:610)
The exception message is clear: 异常消息很明确:
Unable to present exception page : getOutputStream() has already been called for this response 无法显示异常页面 :已为此响应调用了getOutputStream()
java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:已为此响应调用了getOutputStream()
at org.apache.catalina.connector.Response. 在org.apache.catalina.connector.Response。 getWriter (Response.java:610) getWriter (Response.java:610)
An IOException
was been thrown and you're rethrowing it as a custom exception which forced the servletcontainer to show the exception page which will use getWriter()
for this. 抛出了IOException
并且您将其重新抛出为自定义异常,这迫使servletcontainer显示将使用getWriter()
的异常页面。 You should in fact let any IOException
go because that's usually a point of no return. 实际上你应该让任何IOException
出现,因为这通常是一个不归路。
An IOException
can for example be thrown during the job when the client aborted the request. 例如,当客户端中止请求时,可以在作业期间抛出IOException
。 The best practice is to not catch IOException
on the Servlet API yourself. 最佳做法是不要自己捕获Servlet API上的IOException
。 It's already declared in throws
clause of the servlet methods. 它已经在servlet方法的throws
子句中声明。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileObject fileObject = getItSomehow();
if (fileObject != null && fileObject.getBinData() != null) {
response.setHeader("Content-disposition", "attachment; filename=\"" + fileObject.getFilename() + "\"");
response.setContentType(fileObject.getFiletype());
response.setContentLength((int)fileObject.getFilesize().intValue());
response.getOutputStream().write(fileObject.getBinData());
} else {
// ???
}
}
You are calling response.getOutputStream()
twice. 您正在调用response.getOutputStream()
两次。 Instead, call it once and assign it to a local variable, then use that variable for your null check and your write
operation. 相反,调用它一次并将其分配给局部变量,然后使用该变量进行空检查和write
操作。
try {
OutputStream out = response.getOutputStream();
if(response !=null && out !=null &&fileObject!=null && fileObject.getBinData() !=null ){
out.write(fileObject.getBinData());
}
} catch (IOException e) {
throw new ApplicationRuntimeException(e);
}
How can response be null? 响应如何为空? Especially after you've already used it? 特别是在你已经使用它之后? Or response.getOutputStream()? 还是response.getOutputStream()? Or fileObject, after you've already tested it for being non-null? 或者fileObject,在你已经测试它为非null之后? And used it? 用过吗? These tests may be doing more harm than good. 这些测试可能弊大于利。
I always defer to well tested code when I can ... 我总是按照经过良好测试的代码推迟...
http://commons.apache.org/fileupload/apidocs/index.html?org/apache/commons/fileupload/util/package-summary.html http://commons.apache.org/fileupload/apidocs/index.html?org/apache/commons/fileupload/util/package-summary.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.