Android應(yīng)用中的斷點下載功能怎么利用HttpURLConnection實現(xiàn)?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
南山ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
HttpCilent 跟 HttpURLConnection 是安卓原生的用來實現(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è)置字符編碼,這個字符編碼表示為頭500個字節(jié):Range: bytes=0-499 表示第二個500字節(jié):Range: bytes=500-999 表示最后500個字節(jié):Range: bytes=-500 表示500字節(jié)以后的范圍:Range: bytes=500- 第一個和最后一個字節(jié):Range: bytes=0-0,-1 同時指定幾個范圍:Range: bytes=500-600,601-999 httpURLConnection.setRequestProperty("Range" , "bytes=" + startOffset + "-"); // 打開到此 URL 引用的資源的通信鏈接(如果尚未建立這樣的連接)。 httpURLConnection.connect(); if(httpURLConnection.getResponseCode() == 206){ //if startOffset ==0 的時候,你就要把你的文件大小保存起來 //獲取文件的大小httpURLConnection.getContentLength(); //當(dāng)你第一次下載的時候,也就是你的起始位置是0的時候,這就是這個文件的總大小,如果bytes=xx 的范圍大于0,那么你獲取的值就是你的文件總大小-bytes //獲取文件輸出流 bin = new BufferedInputStream(httpURLConnection.getInputStream()); //這個是你要保存在那個目錄的位置 File folder= new File(DOWNLOADDIR); //如果文件夾不存在則新建一個文件夾 if(!folder.exists()){ folder.mkdirs(); } // 隨機訪問文件,可以指定斷點續(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可以用來實現(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è)置字符編碼 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)容,你們掌握Android應(yīng)用中的斷點下載功能怎么利用HttpURLConnection實現(xiàn)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
名稱欄目:Android應(yīng)用中的斷點下載功能怎么利用HttpURLConnection實現(xiàn)
文章出自:http://chinadenli.net/article8/jgihop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、企業(yè)建站、網(wǎng)站內(nèi)鏈、全網(wǎng)營銷推廣、面包屑導(dǎo)航、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)