使用 XSL 转换器的 GZip HttpResponse
我的 Servlet 中有以下代码,但是当 IE 点击该页面时,它返回一个空白的 html 页面。 如果我直接在 StreamResult 构造函数中使用 response.getOutputStream() ,则页面加载正常。 我缺少什么?
response 是 HttpServletResponse 的实例,xsl 是来自 XSLTC TransformerFactory 的 Transformer 实例
response.setHeader("Content-Encoding", "gzip");
GZIPOutputStream gzipOut = new GZIPOutputStream(response.getOutputStream());
Result outputResult = new StreamResult(gzipOut);
xsl.transform(xmlSource, outputResult);
I have the following code below in my Servlet, but when IE hits the page, it returns a blank html page. If I use the response.getOutputStream() directly in the StreamResult constructor, the page loads fine. What am I missing?
response is an instance of HttpServletResponse and xsl is an instance of Transformer from XSLTC TransformerFactory
response.setHeader("Content-Encoding", "gzip");
GZIPOutputStream gzipOut = new GZIPOutputStream(response.getOutputStream());
Result outputResult = new StreamResult(gzipOut);
xsl.transform(xmlSource, outputResult);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜测您没有关闭 gzipOut 流,因此它不会发送“页脚”信息。
I'm going to guess that you aren't closing the gzipOut stream and therefore it isn't sending the "footer" information.
结果发现该流上需要一个 .finish() 。 它与flush类似,但由于它是不同的调用,所以Transformer不知道如何使用它。
Turns out there is a .finish() on this stream that is required. It is similar to flush, but since it is a different call, the Transformer does not know to use it.