android为什么con.getcontentlength总得
答案:2 悬赏:10 手机版
解决时间 2021-02-23 02:48
- 提问者网友:wodetian
- 2021-02-22 19:00
android为什么con.getcontentlength总得
最佳答案
- 五星知识达人网友:杯酒困英雄
- 2021-02-22 19:46
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");
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");
全部回答
- 1楼网友:由着我着迷
- 2021-02-22 20:14
额
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯