2010-08-06 60 views
19

Đây là sự tiếp tục của my previous question mà tôi đã đăng khi tôi không phải là người dùng đã đăng ký. Là một người bồi dưỡng, tôi đang cố gắng tiếp tục tải xuống một tệp lớn từ tài khoản Yahoo! máy chủ trang web khi tải xuống bị gián đoạn. Trước đây tôi nghĩ rằng sự gián đoạn là do giới hạn thời gian chờ là 100 giây (vì Yahoo thực thi giới hạn thời gian đó đối với người dùng viết kịch bản). Tuy nhiên, khi tôi đo thời gian của các ngắt tải xuống, tôi thấy rằng thời gian gián đoạn thay đổi rất nhiều (đôi khi tải xuống không bị gián đoạn trong ít hơn 100 giây và đôi khi lên đến bảy phút). Vì vậy, tôi không biết lý do cho thời gian chờ và tôi chỉ cố gắng để làm việc xung quanh họ.cách tiếp tục tải xuống bị gián đoạn - phần 2

Tôi đã thử đề xuất của naikus (cảm ơn bạn) và, theo kết xuất của các trường tiêu đề http, có vẻ như là tài khoản Yahoo! máy chủ trang web không nhận ra thuộc tính "phạm vi" cho phép tải xuống tiếp tục tại điểm bù của gián đoạn. Thật không may, mặc dù phạm vi byte xuất hiện chính xác trong tiêu đề http trong các kết nối được tiếp tục, nhưng nội dung được truyền luôn khởi động lại ở đầu tệp. (Tệp thử nghiệm của tôi là một mảng gồm 50.000 số nguyên 4 byte tăng dần bắt đầu từ 0. Tệp được tải xuống của tôi luôn bắt đầu đếm lại tại 0 ở mỗi lần bù đắp mà tại đó một gián đoạn tải xuống đã xảy ra.)

Có một số thuộc tính kết nối http khác yêu cầu tôi nên thực hiện để có được Yahoo! máy chủ để thực sự bỏ qua để bù đắp tập tin được chỉ định trong phạm vi byte của tiêu đề? Dưới đây là đoạn code và những gì nó bãi:

  // Setup connection. 
     URL url = new URL(strUrl[0]); 
     URLConnection connection = url.openConnection(); 
     downloaded = Integer.parseInt(strUrl[3]); 
     if (downloaded > 0) { 
      connection.setRequestProperty("Range", "bytes="+downloaded+"-"); 
      connection.connect(); 
      fileLength = mDownloadFileLength; 
      Log.d("AsyncDownloadFile", 
       "new download seek: " + downloaded + 
       "; lengthFile: " + fileLength); 
     } 
     else { 
      connection.connect(); 
      downloaded = 0; 
      fileLength = connection.getContentLength(); 
      mDownloadFileLength = fileLength; 
     } 
     Map<String, List<String>> map = connection.getHeaderFields(); 
     Log.d("AsyncDownloadFile", "header fields: " + map.toString()); 

     // Setup streams and buffers. 
     input = new BufferedInputStream(url.openStream(), 8192); 
     outFile = new RandomAccessFile(strUrl[1], "rw"); 
     if (downloaded > 0) 
      outFile.seek(downloaded); 
     byte data[] = new byte[1024]; 

     // Download file. 
     for (int count=0, i=0; (count=input.read(data, 0, 1024)) != -1; i++) { 
      outFile.write(data, 0, count); 
      downloaded += count; 
      if (downloaded >= fileLength) 
       break; 

      // Display progress. 
      Log.d("AsyncDownloadFile", "bytes: " + downloaded); 
      if ((i%10) == 0) 
       publishProgress((int)(downloaded*100/fileLength)); 
      if (mFlagDisableAsyncTask) { 
       downloaded = 0; 
       break; 
      } 
     } 

     // Close streams. 
     outFile.close(); 
     input.close(); 

bãi:

@ 4:08:24 
D/AsyncDownloadFile(2372): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[2000000], age=[0], serve 
r=[YTS/1.17.13], accept-ranges=[bytes], date=[Fri, 06 Aug 2010 20:08:33 GMT]} 
D/AsyncDownloadFile(2372): bytes: 1024 
D/AsyncDownloadFile(2372): bytes: 1033 
D/AsyncDownloadFile(2372): bytes: 2057 
D/AsyncDownloadFile(2372): bytes: 2493 
D/AsyncDownloadFile(2372): bytes: 3517 
D/AsyncDownloadFile(2372): bytes: 3953 

. 
. 
. 

@ 4:13:25 
D/AsyncDownloadFile(2372): bytes: 386473 
D/AsyncDownloadFile(2372): bytes: 387497 
D/AsyncDownloadFile(2372): bytes: 387933 
D/AsyncDownloadFile(2372): new download seek: 387933; lengthFile: 2000000 
D/AsyncDownloadFile(2372): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[1612067], age=[0], serve 
r=[YTS/1.17.13], accept-ranges=[bytes], date=[Fri, 06 Aug 2010 20:13:29 GMT], co 
ntent-range=[bytes 387933-1999999/2000000]} 
D/AsyncDownloadFile(2372): bytes: 388957 
D/AsyncDownloadFile(2372): bytes: 389981 
D/AsyncDownloadFile(2372): bytes: 390409 
D/AsyncDownloadFile(2372): bytes: 391433 
D/AsyncDownloadFile(2372): bytes: 391869 
D/AsyncDownloadFile(2372): bytes: 392893 

. 
. 
. 

@ 4:18:45 
D/AsyncDownloadFile(2372): bytes: 775413 
D/AsyncDownloadFile(2372): bytes: 775849 
D/AsyncDownloadFile(2372): bytes: 776873 
D/AsyncDownloadFile(2372): bytes: 777309 
D/AsyncDownloadFile(2372): new download seek: 777309; lengthFile: 2000000 
D/AsyncDownloadFile(2372): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[1222691], age=[0], serve 
r=[YTS/1.17.13], accept-ranges=[bytes], date=[Fri, 06 Aug 2010 20:18:54 GMT], co 
ntent-range=[bytes 777309-1999999/2000000]} 
D/dalvikvm(2372): GC_FOR_MALLOC freed 11019 objects/470560 bytes in 155ms 
D/AsyncDownloadFile(2372): bytes: 778333 
D/AsyncDownloadFile(2372): bytes: 779357 
D/AsyncDownloadFile(2372): bytes: 779790 
D/AsyncDownloadFile(2372): bytes: 780814 
D/AsyncDownloadFile(2372): bytes: 781250 
D/AsyncDownloadFile(2372): bytes: 782274 

. 
. 
. 

@ 4:23:45 
D/AsyncDownloadFile(2372): bytes: 1163334 
D/AsyncDownloadFile(2372): bytes: 1163770 
D/AsyncDownloadFile(2372): bytes: 1164794 
D/AsyncDownloadFile(2372): bytes: 1165230 
D/AsyncDownloadFile(2372): new download seek: 1165230; lengthFile: 2000000 
D/AsyncDownloadFile(2372): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[834770], age=[0], server 
=[YTS/1.17.13], accept-ranges=[bytes], date=[Fri, 06 Aug 2010 20:23:47 GMT], con 
tent-range=[bytes 1165230-1999999/2000000]} 
D/AsyncDownloadFile(2372): bytes: 1166246 
D/AsyncDownloadFile(2372): bytes: 1167270 
D/AsyncDownloadFile(2372): bytes: 1167706 
D/AsyncDownloadFile(2372): bytes: 1168730 
D/AsyncDownloadFile(2372): bytes: 1169754 
D/AsyncDownloadFile(2372): bytes: 1170778 

. 
. 
. 

@ 4:30:25 
D/AsyncDownloadFile(2372): bytes: 1551255 
D/AsyncDownloadFile(2372): bytes: 1551691 
D/AsyncDownloadFile(2372): bytes: 1552715 
D/AsyncDownloadFile(2372): bytes: 1553151 
D/AsyncDownloadFile(2372): new download seek: 1553151; lengthFile: 2000000 
D/AsyncDownloadFile(2372): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[446849], age=[0], server 
=[YTS/1.17.13], accept-ranges=[bytes], date=[Fri, 06 Aug 2010 20:30:44 GMT], con 
tent-range=[bytes 1553151-1999999/2000000]} 
D/AsyncDownloadFile(2372): bytes: 1554167 
D/AsyncDownloadFile(2372): bytes: 1554184 
D/AsyncDownloadFile(2372): bytes: 1555208 
D/AsyncDownloadFile(2372): bytes: 1555644 
D/AsyncDownloadFile(2372): bytes: 1556668 
D/AsyncDownloadFile(2372): bytes: 1557104 

. 
. 
. 

@ 4:37:10 
D/AsyncDownloadFile(2372): bytes: 1939188 
D/AsyncDownloadFile(2372): bytes: 1939624 
D/AsyncDownloadFile(2372): bytes: 1940648 
D/AsyncDownloadFile(2372): bytes: 1941084 
D/AsyncDownloadFile(2372): new download seek: 1941084; lengthFile: 2000000 
D/dalvikvm(2372): GC_FOR_MALLOC freed 13701 objects/604600 bytes in 128ms 
D/AsyncDownloadFile(2372): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[58916], age=[0], server= 
[YTS/1.17.13], accept-ranges=[bytes], date=[Fri, 06 Aug 2010 20:37:16 GMT], cont 
ent-range=[bytes 1941084-1999999/2000000]} 
D/AsyncDownloadFile(2372): bytes: 1942108 
D/AsyncDownloadFile(2372): bytes: 1942117 
D/AsyncDownloadFile(2372): bytes: 1943141 
D/AsyncDownloadFile(2372): bytes: 1943577 
D/AsyncDownloadFile(2372): bytes: 1944601 
D/AsyncDownloadFile(2372): bytes: 1945037 

. 
. 
. 

@ 4:38:30 
D/AsyncDownloadFile(2372): bytes: 1993217 
D/AsyncDownloadFile(2372): bytes: 1994241 
D/AsyncDownloadFile(2372): bytes: 1994677 
D/AsyncDownloadFile(2372): bytes: 1995701 
D/AsyncDownloadFile(2372): bytes: 1996137 
D/AsyncDownloadFile(2372): bytes: 1997161 
D/AsyncDownloadFile(2372): bytes: 1997597 
D/AsyncDownloadFile(2372): bytes: 1998621 
D/AsyncDownloadFile(2372): bytes: 1999057 
D/onPostExecute(2372): download: unsuccessful 

- - - 

Sau tip từ BalusC (nhờ), tôi sửa đổi thiết lập kết nối nhưng Yahoo! máy chủ tiếp tục thiết lập lại để bắt đầu của tập tin tại mỗi ngắt. Dưới đây là đoạn code thay đổi và bãi kết quả:

  // Setup connection. 
      URL url = new URL(strUrl[0]); 
      URLConnection connection = url.openConnection(); 
      downloaded = Integer.parseInt(strUrl[3]); 
      if (downloaded == 0) { 
       connection.connect(); 
       strLastModified = connection.getHeaderField("Last-Modified"); 
       fileLength = connection.getContentLength(); 
       mDownloadFileLength = fileLength; 
      } 
      else { 
       connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); 
       connection.setRequestProperty("If-Range", strLastModified); 
       connection.connect(); 
       fileLength = mDownloadFileLength; 
       Log.d("AsyncDownloadFile", 
         "new download seek: " + downloaded + 
         "; lengthFile: " + fileLength); 
      } 
      map = connection.getHeaderFields(); 
      Log.d("AsyncDownloadFile", "header fields: " + map.toString()); 

bãi:

@12:36:40 started 
D/AsyncDownloadFile( 413): header fields: {p3p=[policyref="http://info.yahoo.c 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTP 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA P 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-m 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[2000000], age=[0], serv 
r=[YTS/1.17.13], accept-ranges=[bytes], date=[Sat, 07 Aug 2010 04:36:56 GMT]} 
D/AsyncDownloadFile( 413): bytes: 1024 
D/AsyncDownloadFile( 413): bytes: 2048 
D/AsyncDownloadFile( 413): bytes: 2476 
D/AsyncDownloadFile( 413): bytes: 3500 
D/AsyncDownloadFile( 413): bytes: 3936 

... 

@12:39:20 interrupted 
D/AsyncDownloadFile( 413): bytes: 388068 
D/AsyncDownloadFile( 413): bytes: 389092 
D/AsyncDownloadFile( 413): bytes: 389376 
D/AsyncDownloadFile( 413): new download seek: 389376; lengthFile: 2000000 
D/AsyncDownloadFile( 413): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[1610624], age=[0], serve 
r=[YTS/1.17.13], accept-ranges=[bytes], date=[Sat, 07 Aug 2010 04:39:21 GMT], co 
ntent-range=[bytes 389376-1999999/2000000]} 
D/AsyncDownloadFile( 413): bytes: 390400 
D/AsyncDownloadFile( 413): bytes: 390409 
D/AsyncDownloadFile( 413): bytes: 391433 
D/AsyncDownloadFile( 413): bytes: 391869 

... 

@12:44:10 interrupted 
D/AsyncDownloadFile( 413): bytes: 775413 
D/AsyncDownloadFile( 413): bytes: 775849 
D/AsyncDownloadFile( 413): bytes: 776873 
D/AsyncDownloadFile( 413): bytes: 777309 
D/AsyncDownloadFile( 413): new download seek: 777309; lengthFile: 2000000 
D/AsyncDownloadFile( 413): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[1222691], age=[0], serve 
r=[YTS/1.17.13], accept-ranges=[bytes], date=[Sat, 07 Aug 2010 04:44:20 GMT], co 
ntent-range=[bytes 777309-1999999/2000000]} 
D/dalvikvm( 413): GC_FOR_MALLOC freed 10869 objects/465664 bytes in 122ms 
D/AsyncDownloadFile( 413): bytes: 778333 
D/AsyncDownloadFile( 413): bytes: 778342 
D/AsyncDownloadFile( 413): bytes: 779366 
D/AsyncDownloadFile( 413): bytes: 779802 

... 

@12:49:30 interrupted 
D/AsyncDownloadFile( 413): bytes: 1163782 
D/AsyncDownloadFile( 413): bytes: 1164806 
D/AsyncDownloadFile( 413): bytes: 1165242 
D/AsyncDownloadFile( 413): new download seek: 1165242; lengthFile: 2000000 
D/AsyncDownloadFile( 413): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[834758], age=[0], server 
=[YTS/1.17.13], accept-ranges=[bytes], date=[Sat, 07 Aug 2010 04:49:43 GMT], con 
tent-range=[bytes 1165242-1999999/2000000]} 
D/AsyncDownloadFile( 413): bytes: 1166266 
D/AsyncDownloadFile( 413): bytes: 1167290 
D/AsyncDownloadFile( 413): bytes: 1167718 
D/AsyncDownloadFile( 413): bytes: 1168742 

... 

@12:55:30 interrupted 
D/AsyncDownloadFile( 413): bytes: 1552722 
D/AsyncDownloadFile( 413): bytes: 1553158 
D/AsyncDownloadFile( 413): bytes: 1554182 
D/AsyncDownloadFile( 413): bytes: 1554618 
D/AsyncDownloadFile( 413): new download seek: 1554618; lengthFile: 2000000 
D/AsyncDownloadFile( 413): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[445382], age=[0], server 
=[YTS/1.17.13], accept-ranges=[bytes], date=[Sat, 07 Aug 2010 04:55:39 GMT], con 
tent-range=[bytes 1554618-1999999/2000000]} 
D/AsyncDownloadFile( 413): bytes: 1555642 
D/AsyncDownloadFile( 413): bytes: 1556666 
D/AsyncDownloadFile( 413): bytes: 1557094 
D/AsyncDownloadFile( 413): bytes: 1558118 

... 

@12:57:20 interrupted 
D/AsyncDownloadFile( 413): bytes: 1941834 
D/AsyncDownloadFile( 413): bytes: 1942858 
D/AsyncDownloadFile( 413): bytes: 1943882 
D/AsyncDownloadFile( 413): bytes: 1943994 
D/AsyncDownloadFile( 413): new download seek: 1943994; lengthFile: 2000000 
D/AsyncDownloadFile( 413): header fields: {p3p=[policyref="http://info.yahoo.co 
m/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi 
OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA PO 
L HEA PRE LOC GOV"], content-type=[application/zip], connection=[close], last-mo 
dified=[Fri, 06 Aug 2010 14:47:50 GMT], content-length=[56006], age=[0], server= 
[YTS/1.17.13], accept-ranges=[bytes], date=[Sat, 07 Aug 2010 04:57:15 GMT], cont 
ent-range=[bytes 1943994-1999999/2000000]} 
D/dalvikvm( 413): GC_FOR_MALLOC freed 13617 objects/602200 bytes in 165ms 
D/AsyncDownloadFile( 413): bytes: 1945018 
D/AsyncDownloadFile( 413): bytes: 1946042 
D/AsyncDownloadFile( 413): bytes: 1946470 
D/AsyncDownloadFile( 413): bytes: 1947494 

... 

@12:58:10 finished 
D/AsyncDownloadFile( 413): bytes: 1996103 
D/AsyncDownloadFile( 413): bytes: 1997127 
D/AsyncDownloadFile( 413): bytes: 1997563 
D/AsyncDownloadFile( 413): bytes: 1998587 
D/AsyncDownloadFile( 413): bytes: 1999023 
D/onPostExecute( 413): downloaded: unsuccessful 
+0

Xin chào, tôi có vấn đề với chủ đề của bạn, bạn có thể vui lòng giúp tôi về chủ đề này: https://stackoverflow.com/q/45324253/1830228? –

Trả lời

24

Để khôi phục một tải về, bạn cần phải gửi không chỉ tiêu đề Range yêu cầu, mà còn là tiêu đề If-Range yêu cầu cần chứa mã định danh tệp duy nhất hoặc dấu thời gian sửa đổi tệp.

Nếu máy chủ trả về tiêu đề phản hồi ETag trên bản tải xuống ban đầu, thì bạn nên sử dụng tiêu đề này trong tiêu đề If-Range của các yêu cầu tiếp tục tiếp theo. Hoặc nếu nó trả về tiêu đề phản hồi Last-Modified, thì bạn nên sử dụng tiêu đề này trong tiêu đề yêu cầu If-Range thay thế.

Nhìn vào nhật ký của bạn, máy chủ đã gửi tiêu đề phản hồi Last-Modified. Vì vậy, bạn nên gửi nó trở lại trong tiêu đề If-Range của yêu cầu tiếp tục.

// Initial download. 
String lastModified = connection.getHeaderField("Last-Modified"); 

// ... 

// Resume download. 
connection.setRequestProperty("If-Range", lastModified); 

Máy chủ sẽ sử dụng thông tin này để xác minh xem bạn có yêu cầu chính xác cùng một tệp không.

+0

Cảm ơn bạn đã đề xuất. Trên một ý thích, tôi cũng đã thử thay đổi phần mở rộng của tên tệp đã tải xuống từ tệp .zip (ví dụ: content-type = [application/zip]) thành .dat (ví dụ: content-type = [application/octet-stream]) và Tôi đã thử thay đổi các khóa thuộc tính yêu cầu thành chữ thường. Thật không may, Yahoo! máy chủ tiếp tục tiếp tục tải xuống ở đầu tệp. – gregS

+0

Để không có kết quả, tôi cũng đã thử thay đổi connection.setRequestProperty ("Range", "bytes =" + downloaded + "-"); đến connection.setRequestProperty ("Phạm vi", "byte = -" + (mDownloadFileLength - được tải xuống)); – gregS

+2

Tôi đã nhận được thư trả lời từ Yahoo! hỗ trợ kỹ thuật nói rằng Yahoo! máy chủ không hỗ trợ yêu cầu phạm vi byte: "Web Hosting Yahoo! không hỗ trợ tiêu đề Chấp nhận phạm vi vì chúng tôi làm việc với một nhóm máy chủ và mỗi yêu cầu có khả năng đến một máy chủ khác. Bạn sẽ thấy kết nối = [đóng] trong phản hồi tiêu đề cho biết điều này. " – gregS

5

Có vẻ vấn đề được gọi

input = new BufferedInputStream(url.openStream(), 8192); 

thay vì

input = new BufferedInputStream(connection.getInputStream(), 8192); 

url.openStream() làm cho một cuộc gọi đến openConnection()KHÔNG tài sản phạm vi.

0

Tôi đã quản lý để triển khai tải xuống tiếp tục http trên ứng dụng Java máy khách của mình, mẹo là sử dụng tiêu đề yêu cầu "If-Range: original_ETag" như được đề cập trong câu trả lời được chấp nhận. Dưới đây là thông tin gỡ lỗi tiêu đề đầy đủ, nó có thể giúp bạn hiểu cách thức hoạt động của resume.

Trả lời bình thường từ máy chủ, tệp được đọc từ đầu đến cuối. Giá trị ETag là W/"filesize_bytes-modified_utc" siêu dữ liệu từ tệp đĩa. Nó có thể là một cái gì đó khác nhưng đó là những gì Tomcat sử dụng và thường là tốt nhất cho nội dung tập tin thường xuyên.

HTTP/1.1 200 OK 
ETag: W/"19097900-1410863319978" 
Last-Modified: Tue, 16 Sep 2014 10:28:39 GMT 
Content-Length: 19097900 
Content-Type: application/octet-stream 
Accept-Ranges: bytes 
Server: Apache-Coyote/1.1 
Date: Wed, 17 Sep 2014 10:39:52 GMT 

Khách hàng đọc một đoạn đã cho từ tệp bằng cách thêm tiêu đề Phạm vi và Phạm vi nếu. Phạm vi byte có thể được mở để máy chủ nên đọc bắt đầu từ chỉ mục đã cho đến cuối. Lưu ý Độ dài nội dung không phải là tổng chiều dài của tệp nhưng đoạn byte này.

Range: bytes=10000000- 
If-Range: W/"19097900-1410863319978" 

HTTP/1.1 206 Partial Content 
ETag: W/"19097900-1410863319978" 
Last-Modified: Tue, 16 Sep 2014 10:28:39 GMT 
Content-Length: 9097900 
Content-Type: application/octet-stream 
Accept-Ranges: bytes 
Content-Range: bytes 10000000-19097899/19097900 
Server: Apache-Coyote/1.1 
Date: Wed, 17 Sep 2014 18:12:36 GMT 

Nếu tệp từ xa bị thay đổi thì máy chủ không được trả về 206-PartialContent nhưng trả lời 200-OK thông thường. Máy chủ nên sử dụng giá trị If-Range để kiểm tra tính nhất quán. Một số khách hàng như VLCPlayer đặt trường Phạm vi không có giá trị Dải ô.

Khi khách hàng thực hiện yêu cầu ban đầu mà không có tiêu đề Phạm vi + Nếu-Phạm vi, nó có thể thêm tiêu đề "Nếu được sửa đổi-Từ: sửa đổi_http_date". Máy chủ có thể trả về trạng thái 304-NotModified và ứng dụng khách được thực hiện. Nếu client cung cấp byteStartIdx> = filesize thì máy chủ trả về 416-RequestedRangeNotSatisfiable.

Các lỗi gỡ lỗi bổ sung để làm rõ cách hoạt động của http-resume.

Range=bytes=0-0 
If-Range=W/"19097900-1410863319978" 

HTTP/1.1 206 Partial Content 
ETag=W/"19097900-1410863319978" 
Date=Fri, 19 Sep 2014 12:53:36 GMT 
Content-Length=1 
Last-Modified=Tue, 16 Sep 2014 10:28:39 GMT 
Content-Type=application/octet-stream 
Accept-Ranges=bytes 
Server=Apache-Coyote/1.1 
Content-Range=bytes 0-0/19097900 
- - - - - 

Range=bytes=19097800-29097800 
If-Range=W/"19097900-1410863319978" 

HTTP/1.1 206 Partial Content 
ETag=W/"19097900-1410863319978" 
Date=Fri, 19 Sep 2014 12:59:24 GMT 
Content-Length=100 
Last-Modified=Tue, 16 Sep 2014 10:28:39 GMT 
Content-Type=application/octet-stream 
Accept-Ranges=bytes 
Server=Apache-Coyote/1.1 
Content-Range=bytes 19097800-19097899/19097900 
- - - - - - - - 

Range=bytes=19097899-19097899 
If-Range=W/"19097900-1410863319978" 

HTTP/1.1 206 Partial Content 
ETag=W/"19097900-1410863319978" 
Date=Fri, 19 Sep 2014 13:01:47 GMT 
Content-Length=1 
Last-Modified=Tue, 16 Sep 2014 10:28:39 GMT 
Content-Type=application/octet-stream 
Accept-Ranges=bytes 
Server=Apache-Coyote/1.1 
Content-Range=bytes 19097899-19097899/19097900 
Các vấn đề liên quan