1. 本地创建一个大小跟服务器文件相同大小的临时文件
2. 计算分配几个线程去下载服务器上的资源,知道每个线程下载文件的位置
3. 开启多个线程,每个线程下载对应位置的文件
4. 如果所有的线程都把自己的数据下载完毕了,服务器上的资源就被下载到本地了
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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:orientation="vertical"
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" >
<EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入下载的路径" />
<ProgressBar
android:id="@+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="downLoad"
android:text="下载" />
<TextView
android:id="@+id/tv_process"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载进度" />
</LinearLayout>
package org.gentry.mutildownloader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int DOWN_LOAD_DRROR = 1;
protected static final int SERVER_DRROR = 2;
private static final int DOWN_LOAD_FINISH = 3;
public static final int UPDATE_TEXT = 4;
private EditText et_path;
private ProgressBar pb; // 下载的进度条
private TextView tv_process;
public static int threadCount = 3;
public static int runningThread = 3;
public int currentProcess = 0; // 当前的进度
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case DOWN_LOAD_DRROR:
Toast.makeText(getApplicationContext(), "下载失败",
Toast.LENGTH_SHORT).show();
break;
case SERVER_DRROR:
Toast.makeText(getApplicationContext(), "服务器错误,下载失败",
Toast.LENGTH_SHORT).show();
break;
case DOWN_LOAD_FINISH:
Toast.makeText(getApplicationContext(), "文件下载完毕",
Toast.LENGTH_SHORT).show();
break;
case UPDATE_TEXT:
tv_process.setText("当前进度:" + pb.getProgress() * 100
/ pb.getMax());
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
pb = (ProgressBar) findViewById(R.id.pb);
tv_process = (TextView) findViewById(R.id.tv_process);
}
public void downLoad(View view) {
final String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "下载路径错误", Toast.LENGTH_SHORT).show();
}
new Thread() {
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
// 服务器返回的数据的长度实际上就是文件的长度
int length = conn.getContentLength();
pb.setMax(length); // 设置进度条的最大值
System.out.println("文件总长度:" + length);
// 在客户端本地创建出来一个大小跟服务器端文件一样大小的临时文件
RandomAccessFile raf = new RandomAccessFile(
"/sdcard/setup.exe", "rwd");
// 指定创建的这个文件的长度
raf.setLength(length);
raf.close();
// 假设是3个线程去下载资源
// 平均每一个线程下载的文件的大小
int blockSize = length / threadCount;
for (int threadId = 1; threadId <= threadCount; threadId++) {
// 第一个线程下载的开始位置
int startIndex = (threadId - 1) * blockSize;
int endIndex = threadId * blockSize - 1;
if (threadId == threadCount) {
// 最后一个线程下载的长度要稍微长一点
endIndex = length;
}
System.out.println("线程:" + threadId + "下载:---"
+ startIndex + "--->" + endIndex);
new DownloadThread(path, threadId, startIndex,
endIndex).start();
}
} else {
System.out.println("服务器错误");
Message msg = new Message();
msg.what = SERVER_DRROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = DOWN_LOAD_DRROR;
handler.sendMessage(msg);
}
};
}.start();
}
/**
* 下载文件的子线程,每一个线程下载对应位置的文件
*
* @author Administrator
*
*/
public class DownloadThread extends Thread {
private int threadId;
private int startIndex;
private int endIndex;
private String path;
/**
*
* @param path
* 路径
* @param threadId
* 线程id
* @param startIndex
* 线程下载的开始位置
* @param endIndex
* 线程下载的结束位置
*/
public DownloadThread(String path, int threadId, int startIndex,
int endIndex) {
this.path = path;
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
// 检查是否存在记录下载长度的文件,如果存在读取这个文件的数据
// ---------替换成纯数据库------------
File tempFile = new File("/sdcard/" + threadId + ".txt");
if (tempFile.exists() && tempFile.length() > 0) {
FileInputStream fis = new FileInputStream(tempFile);
byte[] temp = new byte[1024];
int leng = fis.read(temp);
String downloadLen = new String(temp, 0, leng);
int downloadlenInt = Integer.parseInt(downloadLen);
int alreadyDownloadint = downloadlenInt - startIndex;
currentProcess += alreadyDownloadint; // 计算上次断电已经下载文件的长度
startIndex = downloadlenInt; // 修改下载的真实的开始位置
fis.close();
}
// ----------------------------------
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
// 重要:请求服务器下载部分的文件指定文件的位置
conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
+ endIndex);
System.out.println("线程真实下载:" + threadId + "下载:---" + startIndex
+ "--->" + endIndex);
conn.setConnectTimeout(5000);
int code = conn.getResponseCode(); // 服务器请求全部资源200 ok
// 如果从服务器请求部分资源206 ok
if (code == 206) {
InputStream is = conn.getInputStream(); // 已经设置了请求的位置
RandomAccessFile raf = new RandomAccessFile(
"/sdcard/setup.exe", "rwd");
// 随机写文件的时候从哪个位置开始写
raf.seek(startIndex); // 定位文件
int len = 0;
byte[] buffer = new byte[1024];
int total = 0; // 已经下载的数据长度
while ((len = is.read(buffer)) != -1) {
RandomAccessFile file = new RandomAccessFile("/sdcard/"
+ threadId + ".txt", "rwd"); // 作用:记录当前线程下载的数据长度
raf.write(buffer, 0, len);
total += len;
file.write((total + startIndex + "").getBytes()); // 记录的是下载位置
file.close();
// 更新进度条
synchronized (MainActivity.this) {
currentProcess += len; // 获取所有线程下载的总进度
pb.setProgress(currentProcess); // 更改界面上progressBar进度条的进度
// 特殊情况:progressbar,progressdialog
// 进度条对话框可以直接在子线程里面更新UI,内部代码特殊处理
Message msg = Message.obtain(); // 复用旧的消息,避免创建新的消息
msg.what = UPDATE_TEXT;
handler.sendMessage(msg);
}
}
is.close();
raf.close();
System.out.println("线程:" + threadId + "下载完毕");
} else {
System.out.println("线程:" + threadId + "下载失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadFinish();
}
}
private synchronized void threadFinish() {
runningThread--;
if (runningThread == 0) { // 所有的线程已经执行完毕了
for (int i = 1; i <= 3; i++) {
File file = new File("/sdcard/" + i + ".txt");
file.delete();
}
System.out.println("文件下载完毕,删除所有的下载记录");
Message msg = new Message();
msg.what = DOWN_LOAD_FINISH;
handler.sendMessage(msg);
}
}
}
}