Android中http断点下载,Android HttpURLConnection断点下载(单线程)
HttpCilent 跟 HttpURLConnection 是安卓原生的用來實現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();
//設置請求方式
httpURLConnection.setRequestMethod("GET");
//設置字符編碼,這個字符編碼表示為頭500個字節:Range: bytes=0-499
表示第二個500字節:Range: bytes=500-999
表示最后500個字節:Range: bytes=-500
表示500字節以后的范圍:Range: bytes=500-
第一個和最后一個字節: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();
//當你第一次下載的時候,也就是你的起始位置是0的時候,這就是這個文件的總大小,如果bytes=xx 的范圍大于0,那么你獲取的值就是你的文件總大小-bytes
//獲取文件輸出流
bin = new BufferedInputStream(httpURLConnection.getInputStream());
//這個是你要保存在那個目錄的位置
File folder= new File(DOWNLOADDIR);
//如果文件夾不存在則新建一個文件夾
if(!folder.exists()){
folder.mkdirs();
}
// 隨機訪問文件,可以指定斷點續傳的起始位置
//flieAbsolutePath 是你具體的文件路徑
RandomAccessFile randomAccessFile = new RandomAccessFile(flieAbsolutePath,"rwd");
// rwd 跟 r 跟 w的區別是rwd:邊讀編寫邊下載 r讀 w寫
randomAccessFile.seek(startOffset);
byte[] buffer = new byte[2048];
int len;
//isStop可以用來實現暫停功能
while ((len = bin.read(buffer)) != -1 && !isStop) {
randomAccessFile.write(buffer,len);
startOffset += len;
//刷新下載進度
Message msg = new Message();
msg.what = (int)((startOffset * 100) / fileSize);
//使用handler發送消息刷新UI
handler.sendMessage(msg);
//保存下載的位置到SharedPreferences,下次下載的時候拿值寫入設置字符編碼
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);
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持編程小技巧。
總結
如果覺得編程之家網站內容還不錯,歡迎將編程之家網站推薦給程序員好友。
本圖文內容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
小編個人微信號 jb51ccc
喜歡與人分享編程技術與工作經驗,歡迎加入編程之家官方交流群!
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Android中http断点下载,Android HttpURLConnection断点下载(单线程)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 星爵新剧《终极名单》开播 和星爵一起干爆
- 下一篇: 网传《阿凡达:潘多拉边境》11月18日推