HttpURLConnection getContentLength();返回時-1或者是0


出現問題:當getContentLength();返回時-1或者是0時候。

解決辦法:需加上conn.setRequestProperty("Accept-Encoding", "identity");

解釋:在默認情況下,HttpURLConnection 使用 gzip方式獲取,文件 getContentLength()這個方法,每次read完成后可以獲得,當前已經傳送了多少數據,而不能用這個方法獲取 需要傳送多少字節的內容,當read() 返回 -1時,讀取完成,由於這個壓縮后的總長度我無法獲取,那么進度條就沒法計算值了。所以要取得長度則,要求http請求不要gzip壓縮

附上一段代碼:

 1                 // 臨時文件檢驗, 是否續傳文件
 2                 filePath = filePath + ".temp";
 3                 long haveDownLength = 0;
 4                 File tempFile = new File(filePath);
 5                 if (tempFile.exists())
 6                     haveDownLength = tempFile.length();
 7 
 8                 conn = (HttpURLConnection) new URL(urlString).openConnection();
 9                 if (haveDownLength > 0)
10                     conn.setRequestProperty("Connection", "Keep-Alive");
11                 conn.setReadTimeout(6000);
12                 conn.setConnectTimeout(3000);;
13                 conn.setRequestMethod("GET");
14                 conn.setRequestProperty("Accept-Encoding", "identity");
15                 conn.connect();
16 
17                 int fileSize = conn.getContentLength();
18                 long countRead = haveDownLength;
19                 if (conn.getResponseCode() == 200) {
20                     InputStream stream = conn.getInputStream();
21                     FileOutputStream fos = new FileOutputStream(filePath, haveDownLength > 0 ? true : false);
22                     int read = 0;
23 
24                     byte buffer[] = new byte[1024];
25                     while ((read = stream.read(buffer)) >= 0) {
26                         countRead += read;
27                         fos.write(buffer, 0, (int) read);
28                     }
29                     fos.flush();
30                     stream.close();
31                     fos.close();
32                 } else {
33                     fileSize = (int) haveDownLength;
34                 }
35                 conn.disconnect();
36 
37                 int index = filePath.indexOf(".temp");
38                 if (index >= 1) {
39                     String tempFilePath = filePath.substring(0, index);
40                     File renameFile = new File(filePath);
41                     File toFile = new File(tempFilePath);
42                     renameFile.renameTo(toFile);
43 
44                     String md5 = ControlDataSourceGlobalUtil.md5sum(tempFilePath);
45                     // MD5校驗是否文件相同
46                     if (md5.compareToIgnoreCase(fileCrc32) != 0){
47                         File tempFilePathFile = new File(tempFilePath);
48                         tempFilePathFile.delete();
49                         return false;
50                     }
51                     return true;
52                 }
53             } catch (IOException e) {
54                 return false;
55             }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM