Android 多線程下載吞加,斷點(diǎn)續(xù)傳
https://github.com/liu8021203/DownloadDemo
實(shí)現(xiàn)原理:
多線程下載:通過(guò)創(chuàng)建一個(gè)正在下載集合,等待下載集合(使用Vector保證同步)叔壤。
private ListwaitingList=newVector<>();
private ListthreadList=newVector<>();
創(chuàng)建一個(gè)數(shù)據(jù)集來(lái)保存要下載的數(shù)據(jù)
private MapdownloadMap=newHashMap<>();
在下載數(shù)據(jù)前先設(shè)置下載等待隊(duì)列
下載核心代碼:
```
/**
* 下載數(shù)據(jù)
*@paramvo
*/
public voiddownload(finalBeanVO vo)
{
setDownloadWaitingList(vo);
if(!loading)
{
newThread(){
@Override
public voidrun() {
try{
//等待數(shù)量
intwaitingSize =0;
//下載數(shù)量
intthreadlistSize =0;
while((waitingSize =waitingList.size()) >0)
{
loading=true;
threadlistSize =threadList.size();
//設(shè)置下載最大數(shù) 同時(shí)下載隊(duì)列要小于等待隊(duì)列
if(threadlistSize
{
String key =waitingList.get(threadlistSize);
if(!threadList.contains(key))
{
threadList.add(key);
BeanVO beanVO =downloadMap.get(key);
File file =newFile(FILE_PATH);
DownloadThread downloadThread =newDownloadThread(beanVO,file,listener);
downloadThread.start();
downloadThreadMap.put(key,downloadThread);
}
}
Thread.sleep(1000);
}
}catch(Exception e)
{
e.printStackTrace();
}
finally{
loading=false;
}
}
}.start();
}
}
```
斷點(diǎn)續(xù)傳:
創(chuàng)建數(shù)據(jù)庫(kù),將每個(gè)時(shí)段的下載的數(shù)據(jù)大小保存到數(shù)據(jù)庫(kù)中口叙, 當(dāng)網(wǎng)斷開時(shí), 使用RandomAccessFile類從當(dāng)前下載的位置繼續(xù)下載嗅战。
代碼如下:
inputStream inputStream =null;
HttpURLConnection connection =null;
RandomAccessFile randomAccessFile =null;
longfileSize =0;
longcompleteSize =0;
try{
fileSize = getResourceSize(fileUrl);
completeSize =vo.getCompleteSize();
URL url =newURL(fileUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(10*1000);
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept-Language","zh-CN");
connection.setRequestProperty("Referer",url.toString());
connection.setRequestProperty("Charset","UTF-8");
connection.setRequestProperty("Range","bytes="+ completeSize +"-"+ fileSize);//設(shè)置獲取實(shí)體數(shù)據(jù)的范圍
connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
connection.setRequestProperty("Connection","Keep-Alive");
if(connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL)
{
vo.setSize(fileSize);
inputStream = connection.getInputStream();
byte[] buffer =new byte[1024];
intoffset =0;
randomAccessFile =newRandomAccessFile(saveFile,"rwd");
randomAccessFile.seek(completeSize);
longcurrentTime =0;
while((offset = inputStream.read(buffer)) != -1&& !isPause)
{
randomAccessFile.write(buffer,0,offset);
completeSize += offset;
if(System.currentTimeMillis() - currentTime >=3000)
{
currentTime = System.currentTimeMillis();
vo.setCompleteSize(completeSize);
listener.onProgress(vo);
}
}
vo.setCompleteSize(completeSize);
if(!isPause)
{
listener.onComplete(vo);
}
else
{
listener.onCancle(vo);
}
}
}
catch(Exception e)
{
e.printStackTrace();
listener.onError(vo);
}
finally{
try{
if(inputStream !=null)
{
inputStream.close();
}
if(randomAccessFile !=null)
{
randomAccessFile.close();
}
if(connection !=null)
{
connection.disconnect();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
```
hi
```