首页 > 安全资讯 >

Android文件下载之断点续传

12-03-07

HttpURLConnection con = null;long mOffset = 0;InputStream is = null;URL url = null;url = new URL("xxxxxxxxxxx url"); con = (HttpURLConnection) url.openConnection(); con.setReq...

 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 
相关文章
最新文章
热点推荐