我需要刷新 servlet 输出流吗?
我是否需要从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不需要。 servlet 容器将为您刷新并关闭它。顺便说一句,关闭已经隐式调用了刷新。
另请参见 Servlet 3.1 规范的第 5.6 章:
在仍然运行 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:
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).
我猜你在其他问题中得到的答案同样适用于这里:如果它是你的流,请刷新并关闭它。否则,除非另有说明,否则流创建者应该这样做。
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.
指出“无需刷新”规则的一个潜在例外:使用 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.)