Java 如何读取 HttpServletReponses 输出流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/701681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I read an HttpServletReponses output stream?
提问by DyreSchlock
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 过滤器,它会在处理和完成后读取响应的内容,并以 XML 或 PDF 或其他格式返回该信息。但我不确定如何从 HttpServletResponse 对象中获取任何信息。我怎样才能得到这些信息?
采纳答案by Brian Agnew
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.
内容变量现在具有输出流。您也可以对二进制内容执行此操作。
回答by Stefan Kendall
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.
我知道这不是直接的答案,但这就是我提出问题的方式。
回答by Brian Agnew
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 iftruesuggests, 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 reallycan'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 来代理您的请求,从原始输出中捕获输出,然后根据需要进行调整。非常讨厌,但是:-(
回答by omilus
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。