【问题标题】:Do I need to flush the servlet outputstream?我需要刷新 servlet 输出流吗?
【发布时间】:2011-06-29 22:37:39
【问题描述】:

我需要从 HttpServletResponse 中“刷新”OutputStream 吗?

我已经从 to Should I close the servlet outputstream? 看到我不需要关闭它,但不清楚是否需要刷新它。我也应该从容器中得到它吗?

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

【问题讨论】:

    标签: java servlets outputstream


    【解决方案1】:

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

    另见Servlet 3.1 specification的第5.6章:

    5.6 响应对象的关闭

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

        • 上面有人提到,如果是你的流,刷新它,如果不是,不要。如果我理解正确,该海报的规则是否适用于这里?
        • @JMADISON:该规则似乎适用于流,但不适用于基于该流的编写器。由于刷新除了吞吐量/性能之外没有任何害处,我建议将其用于HttpServletResponse输出写入器,无论服务器类型如何。
        【解决方案4】:
        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.
        

        【讨论】:

          猜你喜欢
          • 2011-12-09
          • 1970-01-01
          • 2018-07-20
          • 2010-12-22
          • 1970-01-01
          • 1970-01-01
          • 2011-09-07
          • 2012-12-11
          • 1970-01-01
          相关资源
          最近更新 更多