English 中文(简体)
我需要刷新servlet输出流吗?
原标题:Do I need to flush the servlet outputstream?

我需要从HttpServlet响应中“刷新”OutputStream吗?

我已经看到从到我应该关闭servlet输出流吗?我不需要关闭它,但不清楚是否需要刷新它。我应该也从容器中得到它吗?

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException {
   byte[] response = getResponse();
   String responseType = getResponseType();

   response.setContentLength(response.length);
   response.setContentType(responseType);
   response.getOutputStream().write(response);
   response.getOutputStream().flush(); // yes/no/why?
}
最佳回答

你不需要。servlet容器会为你冲洗并关闭它。顺便说一句,close已经隐式调用flush了。

另请参见Servlet 3.1规范

5.6 Closure of Response Object

When a response is closed, the container must immediately flush all remaining content in the response buffer to the client. The following events indicate that the servlet has satisfied the request and that the response object is to be closed:

  • The termination of the service method of the servlet.
  • The amount of content specified in the setContentLength or setContentLengthLong method of the response has been greater than zero and has been written to the response.
  • The sendError method is called.
  • The sendRedirect method is called.
  • The complete method on AsyncContext is called.

只有当您在同一个流上有多个写入程序,并且您希望切换写入程序(例如,具有混合二进制/字符数据的文件),或者您希望在不确定的时间内保持流指针打开(例如,日志文件)时,在仍然运行servlet服务的情况下调用flush通常才是有益的。

问题回答

我猜你在另一个问题中得到的答案也适用于此:如果它是你的流,请刷新并关闭它。否则,除非另有说明,否则流创建者应该这样做。

为了指出“不需要刷新”规则的一个潜在异常:使用IBM WebSphere Application Server并使用响应Writer(而不是OutputStream),我发现我必须刷新它;否则,我的响应数据的最后一部分就丢失了。我认为IBM的HttpServlet响应类确实刷新了OutputStream,但为Writer使用了一个单独的缓冲区,并且没有刷新它。其他应用程序服务器似乎也这样做了。

因此,如果您将响应数据发送到Writer,则刷新它更安全。但没有必要将OutputStream刷新到讨价还价中。

(我本想把它作为一条评论发布,但我没有这样做的名声。)

java.lang.Object
  extended byjava.io.Writer
      extended byjavax.servlet.jsp.JspWriter


close
public abstract void close()
                    throws IOException
Close the stream, flushing it first. 
This method needs not be invoked explicitly for the initial JspWriter as the code generated by the JSP container will automatically include a call to close(). 

Closing a previously-closed stream, unlike flush(), has no effect. 


Throws: 
IOException - If an I/O error occurs

============================

So, DO NOT close the output stream explicitly.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签