android为什么con.getcontentlength总得
发布网友
发布时间:2024-10-23 06:41
我来回答
共1个回答
热心网友
时间:1天前
HttpURLConnection跟服务交互采用了"gzip"压缩。所以下载的fileLegth > HttpURLConnection.getContentLength().参考api:
By default, this implementation of HttpURLConnection requests that servers use gzip compression. Since getContentLength() returns the number of bytes transmitted, you cannot use that method to predict how many bytes can be read from getInputStream(). Instead, read that stream until it is exhausted: whenread() returns -1.
api上也不推荐是用该方法来验证文件的完整性。可目前项目有不能修改服务器。通过继续研究api发现这种gzip压缩方式是可以取消的。取消办法这http request的head中设置如下参数即可:
urlConnection.setRequestProperty("Accept-Encoding", "identity");