Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android_05_多線程斷點續傳下載

Android_05_多線程斷點續傳下載

編輯:關於Android編程

MainActivity.java

 

package com.itheima.mobilemultidownload;

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.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {

	static int ThreadCount = 3;
	static int finishedThread = 0;
	
	int currentProgress;
	String fileName = QQPlayer.exe;
	//確定下載地址
	String path = http://192.168.13.13:8080/ + fileName;
	private ProgressBar pb;
	TextView tv;
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			//把變量改成long,在long下運算,不然當文件很大時,文本進度就會出現負數的情況
			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() {
				//發送get請求,請求這個地址的資源
				try {
					//這次請求是為了獲取文件的大小,以方便設置虛擬的臨時文件的大小
					URL url = new URL(path);
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();
					conn.setRequestMethod(GET);
					conn.setConnectTimeout(5000);
					conn.setReadTimeout(5000);
					
					if(conn.getResponseCode() == 200){
						//拿到所請求資源文件的長度
						int length = conn.getContentLength();
						
						//設置進度條的最大值就是原文件的總長度
						pb.setMax(length);
						
						File file = new File(Environment.getExternalStorageDirectory(), fileName);
						//生成臨時文件,其中rwd表示直接寫入磁盤,而不是在中途將其緩存一部分,這防止一斷電,有些數據還未寫入磁盤;
						RandomAccessFile raf = new RandomAccessFile(file, rwd);
						//事先設置好臨時文件的大小
						raf.setLength(length);
						//最後記得將其關閉
						raf.close();
						//計算出每個線程應該下載多少字節
						int size = length / ThreadCount;
						
						for (int i = 0; i < ThreadCount; i++) {
							//計算線程下載的開始位置和結束位置
							int startIndex = i * size;
							int endIndex = (i + 1) * size - 1;
							//如果是最後一個線程,那麼結束位置寫死
							if(i == ThreadCount - 1){
								endIndex = length - 1;
							}
//							System.out.println(線程 + i + 的下載區間是: + startIndex + --- + endIndex);
							//new DownLoadThread()是在for()循環裡邊的,所以有三個線程,這就對了!
							new DownLoadThread(startIndex, endIndex, i).start();
						}
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		t.start();
	}
	
	
	class DownLoadThread extends Thread{
		int startIndex;
		int endIndex;
		int threadId;
		
		public DownLoadThread(int startIndex, int endIndex, int threadId) {
			super();
			this.startIndex = startIndex;
			this.endIndex = endIndex;
			this.threadId = threadId;
		}

		@Override
		public void run() {
			//再次發送http請求,下載原文件
			try {
				//生成一個.txt文件是為了保存上次下載完文件後的結束索引,然後把
				File progressFile = new File(Environment.getExternalStorageDirectory(), threadId + .txt);
				//判斷進度臨時文件是否存在
				if(progressFile.exists()){
					FileInputStream fis = new FileInputStream(progressFile);
					BufferedReader br = new BufferedReader(new InputStreamReader(fis));
					//從進度臨時文件中讀取出上一次下載的總進度,然後與原本的開始位置相加,得到新的開始位置
					int lastProgress = Integer.parseInt(br.readLine());
					startIndex += lastProgress;
					
					//把上次下載的進度顯示至進度條
					currentProgress += lastProgress;
					//關於進度條的刷新,可以在子線程中顯示,具體自己以後去分析其原因,
					//應該是在其內部做了相應的處理
					pb.setProgress(currentProgress);
					
					//發送消息,讓主線程刷新文本進度
					handler.sendEmptyMessage(1);
					fis.close();
				}
				System.out.println(線程 + threadId + 的下載區間是: + startIndex + --- + endIndex);
				HttpURLConnection conn;
				URL url = new URL(path);
				conn = (HttpURLConnection) url.openConnection();
				conn.setRequestMethod(GET);
				conn.setConnectTimeout(5000);
				conn.setReadTimeout(5000);
				//設置本次http請求所請求的數據的區間
				conn.setRequestProperty(Range, bytes= + startIndex + - + endIndex);
				
				//請求部分數據,相應碼是206
				if(conn.getResponseCode() == 206){
					//流裡此時只有1/3原文件的數據
					InputStream is = conn.getInputStream();
					byte[] b = new byte[1024];
					int len = 0;
					int total = 0;
					//拿到臨時文件的輸出流
					File file = new File(Environment.getExternalStorageDirectory(), fileName);
					RandomAccessFile raf = new RandomAccessFile(file, rwd);
					//把文件的寫入位置移動至startIndex,
					//這就是我們為什麼使用RandomAccessFile而不用FileOutputStream的原因,
					//因為其seek方法可以很容易在文件中定位
					raf.seek(startIndex);
					while((len = is.read(b)) != -1){
						//每次讀取流裡數據之後,同步把數據寫入臨時文件
						raf.write(b, 0, len);
						total += len;
						System.out.println(線程 + threadId + 下載了 + total);
						
						//每次讀取流裡數據之後,把本次讀取的數據的長度顯示至進度條
						currentProgress += len;
						pb.setProgress(currentProgress);
						//發送消息,讓主線程刷新文本進度
						handler.sendEmptyMessage(1);
						
						//生成一個專門用來記錄下載進度的臨時文件
						RandomAccessFile progressRaf = new RandomAccessFile(progressFile, rwd);
						//每次讀取流裡數據之後,同步把當前線程下載的總進度寫入進度臨時文件中
						progressRaf.write((total + ).getBytes());
						progressRaf.close();
					}
					System.out.println(線程 + threadId + 下載完畢-------------------小志參上!);
					raf.close();
					
					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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

activity_main.xml

 

 


注:也可以使用Github上別人寫好的方法,直接調用即可;

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved