Android HttpURLConnection斷點(diǎn)下載(單線程)
HttpCilent 跟 HttpURLConnection 是安卓原生的用來實(shí)現(xiàn)http請求的類:
Android 6.0之后取消了HttpClient,不支持跟新 ,今天小編使用的是HttpURLConnection :
直接上代碼:
URL url = null; BufferedInputStream bin = null; HttpURLConnection httpURLConnection = null; Context context; try { //你要下載文件的路徑 String urlPath = "MyUrlPath" long fileSize = file.length; //獲取開始下載位置 long startOffset = getFileLength(context); url = new URL(urlPath); //獲取HttpURLConnection對象 httpURLConnection = (HttpURLConnection) url.openConnection(); //設(shè)置請求方式 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 + "-"); // 打開到此 URL 引用的資源的通信鏈接(如果尚未建立這樣的連接)。 httpURLConnection.connect(); if(httpURLConnection.getResponseCode() == 206){ //if startOffset ==0 的時(shí)候,你就要把你的文件大小保存起來 //獲取文件的大小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ī)訪問文件,可以指定斷點(diǎn)續(xù)傳的起始位置 //flieAbsolutePath 是你具體的文件路徑 RandomAccessFile randomAccessFile = new RandomAccessFile(flieAbsolutePath , "rwd"); // rwd 跟 r 跟 w的區(qū)別是rwd:邊讀編寫邊下載 r讀 w寫 randomAccessFile.seek(startOffset); byte[] buffer = new byte[2048]; int len; //isStop可以用來實(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í)候拿值寫入設(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; } /** * 保存文件長度 * @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(); } /** * 獲取文件長度 * @param context * @return */ private static Long getFileLength(Context context){ SharedPreferences sp = context.getSharedPreferences("My_SP" , Context.MODE_PRIVATE); return sp.getLong("File_startOffset" , 0); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 斷點(diǎn)下載和自動安裝的示例代碼
- android多線程斷點(diǎn)下載-帶進(jìn)度條和百分比進(jìn)度顯示效果
- Android原生實(shí)現(xiàn)多線程斷點(diǎn)下載實(shí)例代碼
- 詳解Android中的多線程斷點(diǎn)下載
- Android入門:多線程斷點(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-08Android計(jì)時(shí)器chronometer使用實(shí)例講解
這篇文章主要為大家詳細(xì)介紹了Android計(jì)時(shí)器chronometer使用實(shí)例,介紹了Android計(jì)時(shí)器chronometer基本使用方法,感興趣的小伙伴們可以參考一下2016-04-04Android簡單實(shí)現(xiàn)天氣預(yù)報(bào)App
這篇文章主要為大家詳細(xì)介紹了Android簡單實(shí)現(xiàn)天氣預(yù)報(bào)App,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā))
本篇文章主要介紹了Android上傳多張圖片的實(shí)例代碼(RxJava異步分發(fā)),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Android使用TypeFace設(shè)置TextView的文字字體
這篇文章主要介紹了Android使用TypeFace設(shè)置TextView的文字字體的方法,幫助大家更好的利用Android開發(fā),感興趣的朋友可以了解下2021-01-01