问题描述
应该使用哪种 InputStream 类型来处理将HTTP 内容编码设置为 deflate 的URLConnection流?
What InputStream type should be used to handle URLConnection streams that have HTTP Content-Encoding set to deflate?
对于gzip或zip的内容编码,我使用GZIPInputStream,没问题。
For a Content-Encoding of gzip or zip I use a GZIPInputStream, no problem.
对于deflate的内容编码我尝试使用 InflaterInputStream 和 DeflaterInputStream ,但我得到了
For a Content-Encoding of "deflate" I have tried using InflaterInputStream and DeflaterInputStream but I get
我的理解是deflate编码指的是 Zlib 压缩,并且根据这应该由InflaterInputStream处理。
My understanding is that "deflate" encoding refers to Zlib compression, and according to the docs this should be handled by InflaterInputStream.
推荐答案
在HTTP / 1.1中,内容编码:deflate
实际上是指由定义的DEFLATE压缩算法,包含在zlib数据格式,由定义。
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.
然而,一些供应商只是实现了RFC 1951定义的DEFLATE算法,完全忽略了RFC 1950(没有zlib头文件)。
However some vendors just implement the DEFLATE algorithm as defined RFC 1951, completely ignoring RFC 1950 (no zlib headers).
其他人已被点击同一问题:
Others have been hit by the same issue:
- http://www.mail-archive.com/[email protected]/msg01000.html
- Internet Explorer 8 + Deflate
为了解决这个问题,尝试实例化 InflaterInputStream
传递 Inflater
用t创建的他 nowrap
参数设置为 true
:
In order to work around this, try to instantiate the InflaterInputStream
passing an Inflater
that was created with the nowrap
parameter set to true
:
in = new InflaterInputStream(conn.getInputStream()), new Inflater(true));
这篇关于处理HTTP ContentEncoding“deflate”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!