Android 利用多線程從網絡下載文件


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;


import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends Activity {


String path = "http://169.254.88.88:8080/word.exe";
int threadCount = 3;
int finishedThread = 0;
//所有線程下載總進度
int downloadProgress = 0;
private ProgressBar pb;
private TextView tv;

Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
tv.setText((long)pb.getProgress() * 100 / pb.getMax() + "%");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pb = (ProgressBar) findViewById(R.id.pb);
tv = (TextView) findViewById(R.id.tv);
}




public void click(View v){ //當多次點擊按鈕時,文件將會瞬間下載,因爲再一次調用時,文件的長度被當前長度覆蓋
Thread t = new Thread(){
@Override
public void run() {
//發送http請求,拿到目標文件長度
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);

if(conn.getResponseCode() == 200){
//獲取長度
int length = conn.getContentLength();

//創建臨時文件
File file = new File(Environment.getExternalStorageDirectory(), getNameFromPath(path));
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//設置臨時文件大小與目標文件一致
raf.setLength(length);
raf.close();

//設置進度條的最大值
pb.setMax(length);

//計算每個線程下載區間
int size = length / threadCount;

for (int id = 0; id < threadCount; id++) {
//計算每個線程下載的開始位置和結束位置
int startIndex = id * size;
int endIndex = (id + 1) * size - 1;
if(id == threadCount - 1){
endIndex = length - 1;
}
System.out.println("線程" + id + "下載的區間:" + startIndex + " ~ " + endIndex);
new DownLoadThread(id, startIndex, endIndex).start();
}

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
};
t.start();
}

public String getNameFromPath(String path){
int index = path.lastIndexOf("/");
return path.substring(index + 1);
}

class DownLoadThread extends Thread{

int threadId;
int startIndex;
int endIndex;


public DownLoadThread(int threadId, int startIndex, int endIndex) {
super();
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}




@Override
public void run() {
try {
File fileProgress = new File(Environment.getExternalStorageDirectory(), threadId + ".txt");
int lastProgress = 0;
if(fileProgress.exists()){
//讀取進度臨時文件中的內容
FileInputStream fis = new FileInputStream(fileProgress);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//得到上一次下載進度
lastProgress = Integer.parseInt(br.readLine());
//改變下載的開始位置,上一次下過的,這次就不請求了
startIndex += lastProgress;
fis.close();

//把上一次下載進度加到進度條進度中
downloadProgress += lastProgress;
//發送消息,讓文本進度條改變
handler.sendEmptyMessage(1);
}

//發送http請求,請求要下載的數據
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);
//設置請求數據的區間
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);

//請求部分數據,成功的響應碼是206
if(conn.getResponseCode() == 206){
InputStream is = conn.getInputStream();

byte[] b = new byte[1024];
int len = 0;
//當前線程下載的總進度
int total = lastProgress;
File file = new File(Environment.getExternalStorageDirectory(), getNameFromPath(path));
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//設置寫入的開始位置
raf.seek(startIndex);
while((len = is.read(b)) != -1){
raf.write(b, 0, len);
total += len;
System.out.println("線程" + threadId + "下載了:" + total);


//創建一個進度臨時文件,保存下載進度
RandomAccessFile rafProgress = new RandomAccessFile(fileProgress, "rwd");
//每次下載1024個字節,就,就馬上把1024寫入進度臨時文件
rafProgress.write((total + "").getBytes());
rafProgress.close();

//每次下載len個長度的字節,馬上把len加到下載進度中,讓進度條能反應這len個長度的下載進度
downloadProgress += len;
pb.setProgress(downloadProgress);

//發送消息,讓文本進度條改變
handler.sendEmptyMessage(1);

}
raf.close();
System.out.println("線程" + threadId + "下載完畢------------------");

//3條線程全部下載完畢,纔去刪除進度臨時文件
finishedThread++;

synchronized (path) {
if(finishedThread == threadCount){
for (int i = 0; i < threadCount; i++) {
File f = new File(Environment.getExternalStorageDirectory(), i + ".txt");
f.delete();
}
finishedThread = 0;
}
}
}

}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章