即使提交了响应,是否可以将响应字节写入 HTTPServletResponse 的 OutputStream ?我有一个 Servlet 过滤器,它通过调用 doFilter 将请求转发给 CXFServlet。在 doFilter 之后,我明确地想在 OutputStream 中写入字节。目前,在提交响应后写入 OutputStream 时,我没有得到响应。
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
final HttpServletResponse response = (HttpServletResponse) servletResponse;
final ByteArrayPrintWriter pw = new ByteArrayPrintWriter();
HttpServletResponse wrappedResp = new HttpServletResponseWrapper(
response) {
public PrintWriter getWriter() {
return pw.getWriter();
}
public ServletOutputStream getOutputStream() {
return pw.getStream();
}
};
filterChain.doFilter(servletRequest, wrappedResp);
byte[] bytes = pw.toByteArray();
response.getOutputStream().write(bytes);
response.getOutputStream().flush();
response.getOutputStream().close();
// Do logging after response is sent to client.
}