简体   繁体   English

最佳做法response.getOutputStream

[英]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. 这些测试可能弊大于利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 response.getOutputStream已被调用 - response.getOutputStream has already been called 确保已收到`response.getOutputStream()。write()` - make sure `response.getOutputStream().write()` was received 调用response.getOutputStream()之后,Jasper Reports servlet停止工作。 - The Jasper Reports servlet stopped working after calling response.getOutputStream() 我应该通过response.getOutputStream()显式关闭ZipOutputStream吗? - Should I explicitly close ZipOutputStream over response.getOutputStream()? PdfWriter.getInstance(document,response.getOutputStream())的等效项是什么? 脱颖而出 - what is equivalent for PdfWriter.getInstance(document,response.getOutputStream()); to excel 在JSP中将MySQL请求抛出IllegalStateException时使用的response.getOutputStream - response.getOutputStream used on MySQL request throwing IllegalStateException in JSP 第一次调用时response.getOutputStream()抛出IllegalStateException - response.getOutputStream() throws IllegalStateException when called for the first time spring-boot REST 响应的最佳实践 - Best practice for spring-boot REST response 春季:已经为此响应调用了getOutputStream() - Spring: getOutputStream() has already been called for this response IllegalStateException:已经为此响应调用了 getOutputStream() - IllegalStateException: getOutputStream() has already been called for this response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM