java 处理 HTTP ContentEncoding “deflate”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3932117/
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
Handling HTTP ContentEncoding "deflate"
提问by Joel
What InputStreamtype should be used to handle URLConnection streams that have HTTP Content-Encodingset to deflate?
应该使用什么InputStream类型来处理 HTTP Content-Encoding设置为deflate 的URLConnection 流?
For a Content-Encoding of gzip or zip I use a GZIPInputStream, no problem.
对于 gzip 或 zip 的内容编码,我使用 GZIPInputStream,没问题。
For a Content-Encoding of "deflate" I have tried using InflaterInputStreamand DeflaterInputStreambut I get
对于“deflate”的内容编码,我尝试使用InflaterInputStream和DeflaterInputStream但我得到
java.util.zip.ZipException: unknown compression method at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:147)
java.util.zip.ZipException:java.util.zip.InflaterInputStream.read(InflaterInputStream.java:147) 处的未知压缩方法
My understanding is that "deflate" encoding refers to Zlibcompression, and according to the docsthis should be handled by InflaterInputStream.
我的理解是“deflate”编码是指Zlib压缩,根据文档,这应该由 InflaterInputStream 处理。
回答by Grodriguez
In HTTP/1.1, Content-encoding: deflate
actually refers to the DEFLATE compression algorithm, as defined by RFC 1951, wrapped in the zlib data format, as defined by RFC 1950.
在 HTTP/1.1 中,Content-encoding: deflate
实际上是指RFC 1951定义的 DEFLATE 压缩算法,封装在 zlib 数据格式中,如RFC 1950定义。
However some vendors just implement the DEFLATE algorithm as defined RFC 1951, completely ignoring RFC 1950 (no zlib headers).
然而,一些供应商只是实现了 RFC 1951 定义的 DEFLATE 算法,完全忽略了 RFC 1950(没有 zlib 标头)。
Others have been hit by the same issue:
其他人也遇到了同样的问题:
In order to work around this, try to instantiate the InflaterInputStream
passing an Inflater
that was created with the nowrap
parameter set to true
:
为了解决此问题,请尝试实例化使用设置为的参数创建的InflaterInputStream
传递:Inflater
nowrap
true
in = new InflaterInputStream(conn.getInputStream()), new Inflater(true));
回答by user1706991
Unfortunately, using the InflaterInputStream with an Inflater object did not always produce the correct decompression. I had to detect the headers and tell the Inflater where the offset to the payload was.
不幸的是,将 InflaterInputStream 与 Inflater 对象一起使用并不总是能产生正确的解压。我必须检测标头并告诉 Inflater 负载的偏移量在哪里。
http://thushw.blogspot.com/2014/05/decoding-html-pages-with-content.html
http://thushw.blogspot.com/2014/05/decoding-html-pages-with-content.html