我正在尝试在我们的网站上使用大管道概念。这意味着尝试以块的形式发送响应,而不是整体发送响应,以便用户感觉该页面很快。我通过在 java 中的响应对象上使用flushBuffer方法成功地做到了这一点。但是现在,当我尝试使用 apache mod_deflate 模块压缩内容时,分块丢失了。
这是 apache 用于压缩内容的配置
**
开始 mod_deflate 配置
DeflateBufferSize 100
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog /var/log/httpd/deflate_log deflate
结束 mod_deflate 配置**
这是apache中打开deflate时的响应头
连接:保持活动状态
内容编码:gzip
内容长度:7916
内容类型:text/html;字符集=UTF-8
日期:2012 年 1 月 27 日星期五 20:11:11 GMT
保持活动:超时=300,最大值=3997
服务器:阿帕奇
变化:接受编码
apache 中关闭 deflate 时的响应头
连接:保持活动状态
内容类型:text/html;字符集=UTF-8
日期:2012 年 1 月 27 日星期五 20:21:14 GMT
保持活动:超时=300,最大值=3997
服务器:Apache/2.2.3(CentOS)
传输编码:分块
正如您在上面 2 个标头中看到的,只有在压缩关闭时分块才起作用。我在互联网上搜索了这方面的内容,人们建议减少压缩缓冲区大小价值。我将值减少到 100 个字节,正如您在我的 apache 配置中看到的那样,但这仍然没有解决问题。 DeflateBufferSize 设置为 100 字节意味着响应在 apache 中缓冲,直到接收到 100 字节,然后对其进行压缩。
我正在查看与旧 apache 1.3 捆绑在一起的 mod_gzip 模块,该模块有一个以下指令,允许对分块内容进行 gzip 压缩。
mod_gzip_dechunk 是
有谁知道与 apache 2.x 捆绑在一起的 mod_deflate 中有这样的指令吗?
或者有人知道如何压缩分块内容吗?
事实上我找到了解决方案。我曾经每次创建一个新的 GZipOutputStream 对象来刷新不同的块。相反,您应该仅创建一个 GZipOutputStream 对象,然后使用该对象来压缩响应的所有块。我还对 GZipOutputStream 进行了包装。这是我通过谷歌搜索得到的包装。
public class GZIPFlushableOutputStream extends GZIPOutputStream {
public GZIPFlushableOutputStream(final OutputStream out) throws IOException {
// Using Deflater with nowrap == true will ommit headers and trailers
super(out);
}
private static final byte[] EMPTYBYTEARRAY = new byte[0];
/**
* Insure all remaining data will be output.
*/
public void flush() throws IOException {
/**
* Now this is tricky: We force the Deflater to flush its data by
* switching compression level. As yet, a perplexingly simple workaround
* for
*
* http://developer.java.sun.com/developer/bugParade/bugs/42557 43.html
*/
def.setInput(EMPTYBYTEARRAY, 0, 0);
def.setLevel(Deflater.NO_COMPRESSION);
deflate();
def.setLevel(Deflater.DEFAULT_COMPRESSION);
deflate();
out.flush();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)