如何读取HttpServletReponses输出流?

时间:2020-12-14 22:50:09

I want to make a Servlet filter that will read the contents of the Response after it's been processed and completed and return that information in XML or PDF or whatever. But I'm not sure how to get any information out of the HttpServletResponse object. How can I get at this information?

我想创建一个Servlet过滤器,它将在处理完成后读取Response的内容,并以XML或PDF或其他方式返回该信息。但我不知道如何从HttpServletResponse对象中获取任何信息。我怎样才能获得这些信息?

4 个解决方案

#1


Add this to the filter java file.

将其添加到过滤器java文件中。

static class MyHttpServletResponseWrapper 
  extends HttpServletResponseWrapper {

  private StringWriter sw = new StringWriter(BUFFER_SIZE);

  public MyHttpServletResponseWrapper(HttpServletResponse response) {
    super(response);
  }

  public PrintWriter getWriter() throws IOException {
    return new PrintWriter(sw);
  }

  public ServletOutputStream getOutputStream() throws IOException {
    throw new UnsupportedOperationException();
  }

  public String toString() {
    return sw.toString();
  }
}

Use the follow code:

使用以下代码:

HttpServletResponse httpResponse = (HttpServletResponse) response;
MyHttpServletResponseWrapper wrapper = 
  new MyHttpServletResponseWrapper(httpResponse);

chain.doFilter(request, wrapper);

String content = wrapper.toString();

The content variable now has the output stream. You can also do it for binary content.

内容变量现在具有输出流。您也可以为二进制内容执行此操作。

#2


Spring now has feature for it . All you need to do is use [ContentCachingResponseWrapper], which has method public byte[] getContentAsByteArray() .

Spring现在有它的特色。您需要做的就是使用[ContentCachingResponseWrapper],它具有方法public byte [] getContentAsByteArray()。

I Suggest to make WrapperFactory which will allow to make it configurable, whether to use default ResponseWrapper or ContentCachingResponseWrapper.

我建议使WrapperFactory允许使其可配置,无论是使用默认的ResponseWrapper还是ContentCachingResponseWrapper。

#3


I don't much know that you can get data out of an HttpServletResponse object as such. It may make more sense to structure your application such that requests are proxied to the appropriate handlers and passed about with data transfer objects, from which you can build the appropriate final response. In this manner, you never modifiy more than one response object or need to read from such.

我不太清楚你可以从HttpServletResponse对象中获取数据。构造应用程序可能更有意义,以便将请求代理到适当的处理程序并传递给数据传输对象,从中可以构建适当的最终响应。通过这种方式,您永远不会修改多个响应对象或需要从中读取。

Not a direct answer, I know, but that's how I'd do it give the question.

我知道,这不是一个直接的答案,但我就是这样做的。

#4


I don't believe you can necessarily do this given that writing to the output stream may result in the data being flushed to the client prior to any servlet filters being invoked post-population. As iftrue suggests, a different architecture would be advisable, to generate your XML (say) and then regenerate in whatever output format you desire.

鉴于写入输出流可能导致在填充后调用任何servlet过滤器之前将数据刷新到客户端,我不相信你必须这样做。正如iftrue建议的那样,建议使用不同的架构来生成XML(比如说),然后以您想要的任何输出格式重新生成。

EDIT: Having read your response to iftrue's posting, if you really can't interfere with the current processing, perhaps you require a servlet to proxy your request, capture the output from the original output, and then munge as appropriate. Very nasty, however :-(

编辑:读过你对iftrue发布的回复,如果你真的不能干扰当前的处理,也许你需要一个servlet代理你的请求,从原始输出中捕获输出,然后适当地进行munge。但非常讨厌:-(

#1


Add this to the filter java file.

将其添加到过滤器java文件中。

static class MyHttpServletResponseWrapper 
  extends HttpServletResponseWrapper {

  private StringWriter sw = new StringWriter(BUFFER_SIZE);

  public MyHttpServletResponseWrapper(HttpServletResponse response) {
    super(response);
  }

  public PrintWriter getWriter() throws IOException {
    return new PrintWriter(sw);
  }

  public ServletOutputStream getOutputStream() throws IOException {
    throw new UnsupportedOperationException();
  }

  public String toString() {
    return sw.toString();
  }
}

Use the follow code:

使用以下代码:

HttpServletResponse httpResponse = (HttpServletResponse) response;
MyHttpServletResponseWrapper wrapper = 
  new MyHttpServletResponseWrapper(httpResponse);

chain.doFilter(request, wrapper);

String content = wrapper.toString();

The content variable now has the output stream. You can also do it for binary content.

内容变量现在具有输出流。您也可以为二进制内容执行此操作。

#2


Spring now has feature for it . All you need to do is use [ContentCachingResponseWrapper], which has method public byte[] getContentAsByteArray() .

Spring现在有它的特色。您需要做的就是使用[ContentCachingResponseWrapper],它具有方法public byte [] getContentAsByteArray()。

I Suggest to make WrapperFactory which will allow to make it configurable, whether to use default ResponseWrapper or ContentCachingResponseWrapper.

我建议使WrapperFactory允许使其可配置,无论是使用默认的ResponseWrapper还是ContentCachingResponseWrapper。

#3


I don't much know that you can get data out of an HttpServletResponse object as such. It may make more sense to structure your application such that requests are proxied to the appropriate handlers and passed about with data transfer objects, from which you can build the appropriate final response. In this manner, you never modifiy more than one response object or need to read from such.

我不太清楚你可以从HttpServletResponse对象中获取数据。构造应用程序可能更有意义,以便将请求代理到适当的处理程序并传递给数据传输对象,从中可以构建适当的最终响应。通过这种方式,您永远不会修改多个响应对象或需要从中读取。

Not a direct answer, I know, but that's how I'd do it give the question.

我知道,这不是一个直接的答案,但我就是这样做的。

#4


I don't believe you can necessarily do this given that writing to the output stream may result in the data being flushed to the client prior to any servlet filters being invoked post-population. As iftrue suggests, a different architecture would be advisable, to generate your XML (say) and then regenerate in whatever output format you desire.

鉴于写入输出流可能导致在填充后调用任何servlet过滤器之前将数据刷新到客户端,我不相信你必须这样做。正如iftrue建议的那样,建议使用不同的架构来生成XML(比如说),然后以您想要的任何输出格式重新生成。

EDIT: Having read your response to iftrue's posting, if you really can't interfere with the current processing, perhaps you require a servlet to proxy your request, capture the output from the original output, and then munge as appropriate. Very nasty, however :-(

编辑:读过你对iftrue发布的回复,如果你真的不能干扰当前的处理,也许你需要一个servlet代理你的请求,从原始输出中捕获输出,然后适当地进行munge。但非常讨厌:-(