细节注意
进度条ProgressBar可以直接在子线程中刷新UI,不需要在主线程中刷新UI
TextView通过子线程刷新UI则需要利用Handler
布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始下载"
android:onClick="click"
/>
<ProgressBar
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
java实现文件
MainActivity.java
package com.ldw.multiDownload;
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.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
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; //初始化下载完成的线程的个数
String fileName = "QQ.exe";
String path = "http://192.168.0.102:8080/" + fileName; //确定下载地址
ProgressBar pb;
int CurrentProgress;
TextView tv;
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg){
//防止溢出设置成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);
//创建随机存储文件
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//设置临时文件的大小
raf.setLength(length);
//关闭raf
raf.close();
//计算出每一个线程下载多少字节
int size = length / ThreadCount;
for(int i = 0; i < ThreadCount; i ++){
//startIndex,endIndex分别代表线程的开始和结束位置
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(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请求下载安装包文件
URL url;
try {
File fileProgress = new File(Environment.getExternalStorageDirectory(), threadId + ".txt");
//判断存储下载进度的临时文件是否存在,
if(fileProgress.exists()){
FileInputStream fis = new FileInputStream(fileProgress);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//从下载进度的临时文件中读取上一次下载的总进度,然后和原来文本的开始位置相加,得到新的下载位置
int lastProgress = Integer.parseInt(br.readLine());
startIndex += lastProgress;
//把上次下载的进度显示到进度条,更新进度条
CurrentProgress += lastProgress;
//主线程刷新文本进度
handler.sendEmptyMessage(1);
fis.close();
}
System.out.println("线程" + threadId + "下载区间是" + startIndex +"====" + endIndex);
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//设置请求数据的区间
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 = 0;
//拿到临时文件的引用
File file = new File(Environment.getExternalStorageDirectory(), fileName);
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//更新文件的写入位置,startIndex
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);
//生成一个专门记录下载进度的临时文件
//File fileProgress = new File(threadId + ".txt");
RandomAccessFile fileProgressraf = new RandomAccessFile(fileProgress, "rwd");
//每一次读取流里面的数据以后,把当前线程下载的总进度写入临时文件中
fileProgressraf.write((total + "").getBytes());
fileProgressraf.close();
}
System.out.println("线程" + threadId + "下载过程结束===========================");
raf.close();
//条线程下载完成以后,清理临时文件
finishedThread++;
//线程安全
synchronized(path){
if(finishedThread == ThreadCount){
for(int i = 0; i < ThreadCount; i++){
File filefinish = new File(Environment.getExternalStorageDirectory(), i + ".txt");
filefinish.delete();
}
finishedThread = 0;
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}