效果图:
布局中:
<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"
tools:context="com.zhangli.mutildownloader.MainActivity" >
<EditText
android:id="@+id/et_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入下载的路径"
android:text="http://192.168.1.109:8080/Yodao.exe" />
<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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载进度" />
</LinearLayout>
代码:
package com.zhangli.duoxiancheng;
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.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
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 {
private static final int DOWN_LOAD_ERROR = 1;
private static final int SERVER_ERROR = 2;
public static final int DOWN_LOAD_OVER = 3;
public static final int TEXTVIEW = 4;
private EditText et_path;
private TextView textView;
private ProgressBar pb;// 进度条
private static int threadCount = 3;
private 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_ERROR:
Toast.makeText(getApplicationContext(), "下載失敗", Toast.LENGTH_SHORT).show();
break;
case SERVER_ERROR:
Toast.makeText(getApplicationContext(), "服務器錯誤,下載失敗", Toast.LENGTH_SHORT).show();
break;
case DOWN_LOAD_OVER:
Toast.makeText(getApplicationContext(), "下载完成", Toast.LENGTH_SHORT).show();
break;
case TEXTVIEW:
textView.setText("当前进度:" + pb.getProgress() * 100 / pb.getMax());
break;
default:
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);
textView = (TextView) findViewById(R.id.textView);
}
public void downLoad(View view) {
final String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "下载路径错误", 0).show();
return;
}
new Thread() {
public void run() {
// 1.连接服务器,获取一个文件,获取稳健的长度,在本地创建一个大小跟服务器文件一样的临时文件。
// String path = "http://192.168.1.137:8080/Setup.exe";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoInput(true);
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_ERROR;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = DOWN_LOAD_ERROR;
handler.sendMessage(msg);
}
}
}.start();
}
// 下载文件的子线程,每一个线程下载对应位置的文件
public class DownloadThread extends Thread {
private int threadId;// 线程id
private int startIndex;// 线程下载的开始位置
private int endIndex;// 线程下载的结束位置
private String path;// 下载文件在服务器上的路经
public DownloadThread(String path, int threadId, int startIndex, int endIndex) {
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.path = path;
}
@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 alreadyDownlodint = downloadlenInt - startIndex;
currentProcess += alreadyDownlodint;// 计算上次断点已经下载的长度,然后加在进度条上
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.
System.out.println("code" + code);
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;// 已经下载的数据长度
// File file = new File("threadId" + ".txt");
// 作用:记录当前线程下载的数据长度
while ((len = is.read(buffer)) != -1) {
RandomAccessFile file = new RandomAccessFile("/sdcard/" + threadId + ".txt", "rwd");
// FileOutputStream fos = new FileOutputStream(file);
raf.write(buffer, 0, len);
total += len;
// System.out.println("线程" + threadId + "total"+total);
file.write((total + startIndex + "").getBytes());
file.close();
/**
* 更新进度条
*/
synchronized (MainActivity.this) {
currentProcess += len;// 获取所有线程下载 的总进度
pb.setProgress(currentProcess);// 更改界面上progressber进度条的进度
// 特殊情况progressbar progressdialog
// 进度条对话框可以直接在子线程里面更新ui 内部代码进行特殊处理
Message msg = Message.obtain();// 复用旧的消息 比new更节省资源
msg.what = TEXTVIEW;
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_OVER;
handler.sendMessage(msg);
}
}
}
}
最后加上权限: