1.首先查询是不是后台传输数据的时候出现的问题
查看服务器响应头文件属性,确实没有这个属性了。。。所以返回为-1
1.1Content Length 在头文件中没有描述,暂时还没有解决方案
1.2Content Long在头文件中有描述,查询客户端调用产生的问题
2.调用时让服务器不要gzip方式压缩:
Java Doc 有对此的描述:
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.
conn.setRequestProperty("Accept-Encoding", "identity");
大致意思是:
默认情况下,服务器使用gzip压缩的HttpURLConnection请求的实现。由于getContentLength()返回传输的字节数,因此不能使用该方法预测可以从getInputStream()读取多少字节。相反,读取该流直到它被耗尽:whenread()返回-1。
如采用okhttp可以在request中添加header
new Request.Builder().get().addheader("Accept-Encoding","identity");