版權聲明:本文為部落客原創文章,轉載請标明出處。 https://blog.csdn.net/chaoyu168/article/details/50971687
多線程下載下傳檔案的過程是:
(1)首先獲得下載下傳檔案的長度,然後設定本地檔案的長度。
HttpURLConnection.getContentLength();//擷取下載下傳檔案的長度
RandomAccessFile file = new RandomAccessFile("QQSetup.exe","rwd");
file.setLength(filesize);//設定本地檔案的長度
(2)根據檔案長度和線程數計算每條線程下載下傳的資料長度和下載下傳位置。
如:檔案的長度為6M,線程數為3,那麼,每條線程下載下傳的資料長度為2M,每條線程開始下載下傳的位置如下圖所示。
例如10M大小,使用3個線程來下載下傳,
線程下載下傳的資料長度 (10%3 == 0 ? 10/3:10/3+1) ,第1,2個線程下載下傳長度是4M,第三個線程下載下傳長度為2M
下載下傳開始位置:線程id*每條線程下載下傳的資料長度 = ?
下載下傳結束位置:(線程id+1)*每條線程下載下傳的資料長度-1=?
(3)使用Http的Range頭字段指定每條線程從檔案的什麼位置開始下載下傳,下載下傳到什麼位置為止,
如:指定從檔案的2M位置開始下載下傳,下載下傳到位置(4M-1byte)為止
代碼如下:HttpURLConnection.setRequestProperty("Range",
"bytes=2097152-4194303");
(4)儲存檔案,使用RandomAccessFile類指定每條線程從本地檔案的什麼位置開始寫入資料。
RandomAccessFile threadfile =
new RandomAccessFile("QQWubiSetup.exe ","rwd");
threadfile.seek(2097152);//從檔案的什麼位置開始寫入資料
/**
*多線程下載下傳,UI更新類
*@author young
* */
public class MultiThreadDownload extends Thread{
private static final String TAG = "MultiThreadDownload";
/**每一個線程需要下載下傳的大小 */
private int blockSize;
/*** 線程數量<br> 預設為5個線程下載下傳*/
private int threadNum = 5;
/*** 檔案大小 */
private int fileSize;
/** * 已經下載下傳多少 */
private int downloadSize;
/**檔案的url,線程編号,檔案名稱*/
private String UrlStr,ThreadNo,fileName;
/***儲存的路徑*/
private String savePath;
/**下載下傳的百分比*/
private int downloadPercent = 0;
/**下載下傳的 平均速度*/
private int downloadSpeed = 0;
/**下載下傳用的時間*/
private int usedTime = 0;
/**目前時間*/
private long curTime;
/**是否已經下載下傳完成*/
private boolean completed = false;
private Handler handler ;
/**
* 下載下傳的構造函數
* @param url 請求下載下傳的URL
* @param handler UI更新使用
* @param savePath 儲存檔案的路徑
*/
public MultiThreadDownload(Handler handler,String url,String savePath)
{
this.handler = handler;
this.UrlStr = url;
this.savePath = savePath;
Log.e(TAG, toString());
}
@Override
public void run() {
FileDownloadThread[] fds = new FileDownloadThread[threadNum];//設定線程數量
try {
URL url = new URL(UrlStr);
URLConnection conn = url.openConnection();
fileSize = conn.getContentLength();
this.fileName = FileUtil.getFileName(UrlStr);
//隻建立一個檔案,saveFile下載下傳内容
File saveFile = new File(savePath+"/"+fileName);
Log.e(TAG, "檔案一共:"+fileSize+" savePath "+savePath+" fileName "+fileName);
RandomAccessFile accessFile = new RandomAccessFile(saveFile,"rwd");
//設定本地檔案的長度和下載下傳檔案相同
accessFile.setLength(fileSize);
accessFile.close();
//Handler更新UI,發送消息
sendMsg(FileUtil.startDownloadMeg);
//每塊線程下載下傳資料
blockSize = ((fileSize%threadNum)==0)?(fileSize/threadNum):(fileSize/threadNum+1);
Log.e(TAG, "每個線程分别下載下傳 :"+blockSize);
for (int i = 0; i < threadNum; i++) {
int curThreadEndPosition = (i+1)!=threadNum ? ((i+1)*blockSize-1) : fileSize;
FileDownloadThread fdt = new FileDownloadThread(url, saveFile, i*blockSize, curThreadEndPosition);
fdt.setName("thread"+i);
fdt.start();
fds[i]=fdt;
}
/**
* 擷取資料,更新UI,直到所有下載下傳線程都下載下傳完成。
*/
boolean finished = false;
//開始時間,放在循環外,求解的usedTime就是總時間
long startTime = System.currentTimeMillis();
while(!finished)
{
downloadSize = 0;
finished = true;
for (int i = 0; i < fds.length; i++) {
downloadSize+= fds[i].getDownloadSize();
if(!fds[i].isFinished())
{
finished = false;
}
}
downloadPercent = (downloadSize*100)/fileSize;
curTime = System.currentTimeMillis();
System.out.println("curTime = "+curTime+" downloadSize = "+downloadSize+" usedTime "+(int) ((curTime-startTime)/1000));
usedTime = (int) ((curTime-startTime)/1000);
if(usedTime==0)usedTime = 1;
downloadSpeed = (downloadSize/usedTime)/1024;
sleep(1000);/*1秒鐘重新整理一次界面*/
sendMsg(FileUtil.updateDownloadMeg);
}
Log.e(TAG, "下載下傳完成");
completed = true;
sendMsg(FileUtil.endDownloadMeg);
} catch (Exception e) {
Log.e(TAG, "multi file error Exception "+e.getMessage());
e.printStackTrace();
}
super.run();
}
/**
* 得到檔案的大小
* @return
*/
public int getFileSize()
{
return this.fileSize;
}
/**
* 得到已經下載下傳的數量
* @return
*/
public int getDownloadSize()
{
return this.downloadSize;
}
/**
* 擷取下載下傳百分比
* @return
*/
public int getDownloadPercent(){
return this.downloadPercent;
}
/**
* 擷取下載下傳速度
* @return
*/
public int getDownloadSpeed(){
return this.downloadSpeed;
}
/**
* 修改預設線程數
* @param threadNum
*/
public void setThreadNum(int threadNum){
this.threadNum = threadNum;
}
/**
* 分塊下載下傳完成的标志
* @return
*/
public boolean isCompleted(){
return this.completed;
}
@Override
public String toString() {
return "MultiThreadDownload [threadNum=" + threadNum + ", fileSize="
+ fileSize + ", UrlStr=" + UrlStr + ", ThreadNo=" + ThreadNo
+ ", savePath=" + savePath + "]";
}
/**
* 發送消息,使用者提示
* */
private void sendMsg(int what)
{
Message msg = new Message();
msg.what = what;
handler.sendMessage(msg);
}
public class FileDownloadThread extends Thread{
private static final String TAG = "FileDownloadThread";
/**緩沖區 */
private static final int BUFF_SIZE = 1024;
/**需要下載下傳的URL*/
private URL url;
/**緩存的FIle*/
private File file;
/**開始位置*/
private int startPosition;
/**結束位置*/
private int endPosition;
/**目前位置*/
private int curPosition;
/**完成*/
private boolean finished = false;
/**已經下載下傳多少*/
private int downloadSize = 0;
/***
* 分塊檔案下載下傳,可以建立多線程模式
* @param url 下載下傳的URL
* @param file 下載下傳的檔案
* @param startPosition 開始位置
* @param endPosition 結束位置
*/
public FileDownloadThread(URL url, File file, int startPosition,
int endPosition) {
this.url = url;
this.file = file;
this.startPosition = startPosition;
this.curPosition = startPosition;
this.endPosition = endPosition;
Log.e(TAG, toString());
}
@Override
public void run() {
BufferedInputStream bis = null;
RandomAccessFile rAccessFile = null;
byte[] buf = new byte[BUFF_SIZE];
URLConnection conn = null;
try {
conn = url.openConnection();
conn.setConnectTimeout(10000);//設定逾時
conn.setReadTimeout(10000);
conn.setAllowUserInteraction(true);
System.out.println(this.getName()+" startPosition "+startPosition+" endPosition "+endPosition);
conn.setRequestProperty("Range", "bytes="+(startPosition)+"-"+endPosition); //取剩餘未下載下傳的
rAccessFile = new RandomAccessFile(file,"rwd");//讀寫
//設定從什麼位置開始寫入資料
rAccessFile.seek(startPosition);
bis = new BufferedInputStream(conn.getInputStream(), BUFF_SIZE);
while(curPosition<endPosition) //目前位置小于結束位置 繼續下載下傳
{
int len = bis.read(buf,0,BUFF_SIZE);
if(len==-1) //下載下傳完成
{
break;
}
rAccessFile.write(buf,0,len);
curPosition = curPosition +len;
if(curPosition > endPosition)
{ //如果下載下傳多了,則減去多餘部分
System.out.println(" curPosition > endPosition !!!!");
int extraLen = curPosition-endPosition;
downloadSize += (len-extraLen+1);
}else{
downloadSize+=len;
}
}
this.finished = true; //目前階段下載下傳完成
Log.e(TAG, "目前"+this.getName()+"下載下傳完成");
} catch (Exception e) {
Log.e(TAG, "download error Exception "+e.getMessage());
e.printStackTrace();
}finally{
//關閉流
FileUtil.closeInputStream(bis);
try {
rAccessFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("AccessFile", "AccessFile IOException "+e.getMessage());
}
}
super.run();
}
/**
* 是否完成目前段下載下傳完成
* @return
*/
public boolean isFinished() {
return finished;
}
/**
* 已經下載下傳多少
* @return
*/
public int getDownloadSize() {
return downloadSize;
}
@Override
public String toString() {
return "FileDownloadThread [url=" + url + ", file=" + file
+ ", startPosition=" + startPosition + ", endPosition="
+ endPosition + ", curPosition=" + curPosition + ", finished="
+ finished + ", downloadSize=" + downloadSize + "]";
}
多線程下載下傳是分段下載下傳,建立儲存一個檔案,子線程分别通過RandomAccessFile類進行寫入操作。
示例源碼:
demo下載下傳