Android2.2以上的版本HttpURLConnection.getContentLength()獲取的size跟下載下來的file的legth不相等

2.2以上的版本下載網絡資源不完整沒法更新。HttpURLConnection.getContentLength()獲取的size跟下載下來的file的legth不等。html

緣由是:HttpURLConnection跟服務交互採用了"gzip"壓縮。因此下載的fileLegth > HttpURLConnection.getContentLength().java

參考api:android


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"); 

 2.2以上的版本默認都是採用壓縮優化,文件的完整性驗證最好仍是用md5。網絡

相關文章
相關標籤/搜索