Android文件下載之斷點續傳

 HttpURLConnection con = null;

long mOffset = 0;

InputStream is = null;

URL url = null;

url = new URL("xxxxxxxxxxx url");
   
    con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setDoInput(true);
    //斷點續傳
    con.setRequestProperty("RANGE", "bytes="+ mOffset +"-");
    con.setReadTimeout(COM.UPGRADE_TIME_OUT);
    con.setConnectTimeout(COM.UPGRADE_TIME_OUT);

is = con.getInputStream();

int len = con.getContentLength();

 

斷點續傳的關鍵在 con.setRequestProperty("RANGE", "bytes="+ mOffset +"-");
當mOffset =0 時獲取的文件大小會是原文件大小,若mOffset增加,則返回的文件大小為剩下的字節數。 

摘自 wchinaw 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *