Android HttpURLConnection斷點(diǎn)下載(單線程)
HttpCilent 跟 HttpURLConnection 是安卓原生的用來(lái)實(shí)現(xiàn)http請(qǐng)求的類:
Android 6.0之后取消了HttpClient,不支持跟新 ,今天小編使用的是HttpURLConnection :
直接上代碼:
URL url = null;
BufferedInputStream bin = null;
HttpURLConnection httpURLConnection = null;
Context context;
try {
//你要下載文件的路徑
String urlPath = "MyUrlPath"
long fileSize = file.length;
//獲取開(kāi)始下載位置
long startOffset = getFileLength(context);
url = new URL(urlPath);
//獲取HttpURLConnection對(duì)象
httpURLConnection = (HttpURLConnection) url.openConnection();
//設(shè)置請(qǐng)求方式
httpURLConnection.setRequestMethod("GET");
//設(shè)置字符編碼,這個(gè)字符編碼表示為頭500個(gè)字節(jié):Range: bytes=0-499
表示第二個(gè)500字節(jié):Range: bytes=500-999
表示最后500個(gè)字節(jié):Range: bytes=-500
表示500字節(jié)以后的范圍:Range: bytes=500-
第一個(gè)和最后一個(gè)字節(jié):Range: bytes=0-0,-1
同時(shí)指定幾個(gè)范圍:Range: bytes=500-600,601-999
httpURLConnection.setRequestProperty("Range" , "bytes=" + startOffset + "-");
// 打開(kāi)到此 URL 引用的資源的通信鏈接(如果尚未建立這樣的連接)。
httpURLConnection.connect();
if(httpURLConnection.getResponseCode() == 206){
//if startOffset ==0 的時(shí)候,你就要把你的文件大小保存起來(lái)
//獲取文件的大小httpURLConnection.getContentLength();
//當(dāng)你第一次下載的時(shí)候,也就是你的起始位置是0的時(shí)候,這就是這個(gè)文件的總大小,如果bytes=xx 的范圍大于0,那么你獲取的值就是你的文件總大小-bytes
//獲取文件輸出流
bin = new BufferedInputStream(httpURLConnection.getInputStream());
//這個(gè)是你要保存在那個(gè)目錄的位置
File folder= new File(DOWNLOADDIR);
//如果文件夾不存在則新建一個(gè)文件夾
if(!folder.exists()){
folder.mkdirs();
}
// 隨機(jī)訪問(wèn)文件,可以指定斷點(diǎn)續(xù)傳的起始位置
//flieAbsolutePath 是你具體的文件路徑
RandomAccessFile randomAccessFile = new RandomAccessFile(flieAbsolutePath , "rwd");
// rwd 跟 r 跟 w的區(qū)別是rwd:邊讀編寫(xiě)邊下載 r讀 w寫(xiě)
randomAccessFile.seek(startOffset);
byte[] buffer = new byte[2048];
int len;
//isStop可以用來(lái)實(shí)現(xiàn)暫停功能
while ((len = bin.read(buffer)) != -1 && !isStop) {
randomAccessFile.write(buffer, 0, len);
startOffset += len;
//刷新下載進(jìn)度
Message msg = new Message();
msg.what = (int)((startOffset * 100) / fileSize);
//使用handler發(fā)送消息刷新UI
handler.sendMessage(msg);
//保存下載的位置到SharedPreferences,下次下載的時(shí)候拿值寫(xiě)入設(shè)置字符編碼
saveFileLength(context , startOffset);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(url != null){
url = null;
}
if(bin != null){
try {
bin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(httpURLConnection != null){
httpURLConnection.disconnect();
}
}
return null;
}
/**
* 保存文件長(zhǎng)度
* @param context
* @param fileLength
*/
private static void saveFileLength(Context context ,Long fileLength ){
SharedPreferences sp = context.getSharedPreferences("My_SP" , Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putLong("File_startOffset" , fileLength);
editor.commit();
}
/**
* 獲取文件長(zhǎng)度
* @param context
* @return
*/
private static Long getFileLength(Context context){
SharedPreferences sp = context.getSharedPreferences("My_SP" , Context.MODE_PRIVATE);
return sp.getLong("File_startOffset" , 0);
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 斷點(diǎn)下載和自動(dòng)安裝的示例代碼
- android多線程斷點(diǎn)下載-帶進(jìn)度條和百分比進(jìn)度顯示效果
- Android原生實(shí)現(xiàn)多線程斷點(diǎn)下載實(shí)例代碼
- 詳解Android中的多線程斷點(diǎn)下載
- Android入門(mén):多線程斷點(diǎn)下載詳細(xì)介紹
- Android使用多線程實(shí)現(xiàn)斷點(diǎn)下載
- Android實(shí)現(xiàn)斷點(diǎn)下載的方法
- Android實(shí)現(xiàn)多線程斷點(diǎn)下載的方法
- Android實(shí)現(xiàn)斷點(diǎn)多線程下載
相關(guān)文章
Android startActivityForResult()代替方案示例
這篇文章主要為大家介紹了Android startActivityForResult()代替方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Android計(jì)時(shí)器chronometer使用實(shí)例講解
這篇文章主要為大家詳細(xì)介紹了Android計(jì)時(shí)器chronometer使用實(shí)例,介紹了Android計(jì)時(shí)器chronometer基本使用方法,感興趣的小伙伴們可以參考一下2016-04-04
Android簡(jiǎn)單實(shí)現(xiàn)天氣預(yù)報(bào)App
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單實(shí)現(xiàn)天氣預(yù)報(bào)App,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android動(dòng)畫(huà)系列之屬性動(dòng)畫(huà)的基本使用教程
這篇文章主要給大家介紹了關(guān)于Android動(dòng)畫(huà)系列教程之屬性動(dòng)畫(huà)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā))
本篇文章主要介紹了Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā)),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Android藍(lán)牙服務(wù)啟動(dòng)流程分析探索
這篇文章主要介紹了Android藍(lán)牙服務(wù)啟動(dòng)流程,了解內(nèi)部原理是為了幫助我們做擴(kuò)展,同時(shí)也是驗(yàn)證了一個(gè)人的學(xué)習(xí)能力,如果你想讓自己的職業(yè)道路更上一層樓,這些底層的東西你是必須要會(huì)的2023-01-01
Android使用TypeFace設(shè)置TextView的文字字體
這篇文章主要介紹了Android使用TypeFace設(shè)置TextView的文字字體的方法,幫助大家更好的利用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-01-01

