我需要刷新 servlet 输出流吗?

发布于 2024-10-18 02:34:23 字数 646 浏览 1 评论 0原文

我是否需要从 HttpServletResponse 中“刷新”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?
}

Do I need to "flush" the OutputStream from the HttpServletResponse?

I already saw from to Should I close the servlet outputstream? that I don't need to close it, but it's not clear if I need to flush it. Should I expect it from the container as well?

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?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烈酒灼喉 2024-10-25 02:34:23

你不需要。 servlet 容器将为您刷新并关闭它。顺便说一句,关闭已经隐式调用了刷新。

另请参见 Servlet 3.1 规范的第 5.6 章:

5.6 响应对象的关闭

当响应关闭时,容器必须立即刷新所有剩余的
向客户端发送响应缓冲区中的内容。以下事件表明 servlet 已满足请求并且响应对象将被关闭:

  • Servlet 的 service 方法终止。
  • setContentLength 中指定的内容量或
    setContentLengthLong 方法的响应已经大于零并且
    已写入响应。
  • 调用 sendError 方法。
  • 调用 sendRedirect 方法。
  • 调用 AsyncContext 上的 complete 方法。

在仍然运行 Servlet 服务的同时调用刷新通常只有在同一个流上有多个写入器并且您想要切换写入器(例如具有混合二进制/字符数据的文件)时,或者当您想要保持流指针打开时才有用不确定的时间(例如日志文件)。

You don't need to. The servletcontainer will flush and close it for you. The close by the way already implicitly calls flush.

See also chapter 5.6 of Servlet 3.1 specification:

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.

Calling flush while still running the servlet's service is usually only beneficial when you have multiple writers on the same stream and you want to switch of the writer (e.g. file with mixed binary/character data), or when you want to keep the stream pointer open for an uncertain time (e.g. a logfile).

朦胧时间 2024-10-25 02:34:23

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

Guess that the same answer you got in your other question applies here: if it is your stream, flush and close it. Otherwise the stream creator should be doing it, unless otherwise stated.

旧故 2024-10-25 02:34:23

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

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

(我本想将此作为评论发布,但缺乏这样做的声誉。)

To point out an insidious exception to the rule “no need to flush”: Working with IBM WebSphere Application Server and using the response Writer (rather than the OutputStream) I found that I had to flush it; otherwise a final portion of my response data was lost. I suppose that IBM's HttpServletResponse class does indeed flush the OutputStream but uses a separate buffer for the Writer and does not flush it. Other application servers seem to do this.

So if you send your response data to the Writer it is safer to flush it. But there is no need to flush the OutputStream into the bargain.

(I would have posted this as a comment but lack the reputation to do it.)

呆头 2024-10-25 02:34:23
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.
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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文