本篇文章主要介绍了"Android多线程下载及断点续传",主要涉及到方面的内容,对于Android开发感兴趣的同学可以参考一下:
本文在上篇的基础上,再Android中实现多线程下载及断点续传。其实,核心代码是完全一致的,不过在Android...
本文在上篇的基础上,再Android中实现多线程下载及断点续传。其实,核心代码是完全一致的,不过在Android中添加了下载进度显示,设计到Android中的消息机制,多线程之间的通信,这些知识网上一搜一箩筐,在此就不再赘述了,直接上代码,代码中有详细的注释。
main.xml
MainActivity.java
package com.demo.multidownload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private ProgressBar pb;
private Button bt;
private TextView tv;
private EditText et;
boolean flag = true;// 标志位,为true说明还在下载,子线程则定期向主线程发送下载进度
boolean stopflag = false;
private File progressfile = null;//记录进度条的文件
// handler在主线程中new出来,主要用于发送和处理消息
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
pb.setProgress(total);// 设置当前进度条的进度值
int max = pb.getMax();
if (total >= max) {
total = max;
flag = false;// 下载完成,停止下载
}
int result = total * 100 / max;//计算显示的进度值,这个其实就是百分制,以百分比的形式显示下载进度
tv.setText("当前进度 :" + result + "%");
super.handleMessage(msg);
}
};
int total = 0;//进度条显示的值
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb = (ProgressBar) this.findViewById(R.id.pb);
bt = (Button) this.findViewById(R.id.bt);
tv = (TextView) this.findViewById(R.id.tv_process);
et = (EditText) this.findViewById(R.id.et);
bt.setOnClickListener(this);
// 记录下载进度的文件,如果是中断下载,则下次重新进入下载界面的话,则读取进度条的进度并显示在进度条上
progressfile = new File("/mnt/sdcard/progress.txt");
try {
// 如果保存进度的文件存在,则读取进度,并设置进度
if (progressfile.exists()) {
FileInputStream fis = new FileInputStream(progressfile);
byte[] result = StreamTool.getBytes(fis);
String str = new String(result);
if (!"".equals(str)) {
//progressfile中的存的数据格式为:当前进度值#要下载文件总大小,为什么要存储文件总大小?
//因为我们设置pb的最大值为下载文件的总大小,只是在显示的时候以百分比的形式显示下载进度
//我们必须设置pb的最大值之后,setProgress才会生效。
int newprogress = Integer.parseInt(str.split("#")[0]);//当前进度值,即上次中断下载时存储的进度条的值
int totalsize = Integer.parseInt(str.split("#")[1]);//文件的总大小,即pb的最大值
pb.setMax(totalsize);// 设置pb的最大值,否则setProgress无效
pb.setProgress(newprogress);
total = newprogress;//将total设置为读取的进度值,这样此次下载的进度就会从上次中断下载的位置开始继续下载
// 设置显示的进度值
tv.setText("当前进度 :" + total * 100 / totalsize + "%");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt:
// 创建一个子线程 定期的更新ui
if ("开始下载".equals(bt.getText().toString())) {
System.out.println("开始下载");
bt.setText("暂停");
stopflag = false; // 开始下载
} else {
System.out.println("暂停下载");
bt.setText("开始下载");
stopflag = true;// 暂停下载
}
new Thread() {
public void run() {
super.run();
while (flag) {
try {
sleep(1000);// 一秒钟更新一次主界面
Message msg = new Message();//向主线程发送消息,通知主线程更新UI,这里就是更新pb的进度值
handler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
// 开始执行下载的操作
String path = et.getText().toString().trim();
if ("".equals(path)) {
Toast.makeText(this, "路径不能为空", 1).show();
return;
}
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
int code = conn.getResponseCode();
if (code == 200) {
int len = conn.getContentLength();
RandomAccessFile file = new RandomAccessFile("/mnt/sdcard/"
+ getFilenName(path), "rwd");
// 1.设置本地文件大小跟服务器的文件大小一致
file.setLength(len);
// 设置进度条的最大值
pb.setMax(len);
// 2 .假设开启3 个线程
int threadnumber = 3;
int blocksize = len / threadnumber;
/**
* 线程1 0~ blocksize
* 线程2 1*bolocksize ~ 2*blocksize
* 线程3 2*blocksize ~ 文件末尾
*/
for (int i = 0; i < threadnumber; i++) {
int startposition = i * blocksize;
int endpositon = (i + 1) * blocksize;
if (i == (threadnumber - 1)) {
// 最后一个线程
endpositon = len;
}
DownLoadTask task = new DownLoadTask(i, path,
startposition, endpositon);
task.start();
}
}
} catch (Exception e) {
Toast.makeText(this, "下载出现异常", 0).show();
e.printStackTrace();
}
break;
}
}
class DownLoadTask extends Thread {
int threadid;
String filepath;
int startposition;
int endpositon;
public DownLoadTask(int threadid, String filepath, int startposition,
int endpositon) {
this.threadid = threadid;
this.filepath = filepath;
this.startposition = startposition;
this.endpositon = endpositon;
}
@Override
public void run() {
try {
// 创建一个文件对象 ,记录当前某个文件的下载位置
File postionfile = new File("/mnt/sdcard/thread" + threadid
+ ".txt");
URL url = new URL(filepath);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
// 如果positionfile存在则说明是断点续传
if (postionfile.exists()) {
FileInputStream fis = new FileInputStream(postionfile);
byte[] result = StreamTool.getBytes(fis);
String str = new String(result);
if (!"".equals(str)) {
int newstartposition = Integer.parseInt(str);
if (newstartposition > startposition) {
startposition = newstartposition;
}
}
}
if (!stopflag) {// 每次开始下载的时候,才打印此Log
System.out.println("线程" + threadid + "正在下载 " + "开始位置 : "
+ startposition + "结束位置 " + endpositon);
}
// "Range", "bytes=2097152-4194303")
conn.setRequestProperty("Range", "bytes=" + startposition + "-"
+ endpositon);
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
InputStream is = conn.getInputStream();
RandomAccessFile file = new RandomAccessFile("/mnt/sdcard/"
+ getFilenName(filepath), "rwd");
// 设置 数据从文件哪个位置开始写
file.seek(startposition);
byte[] buffer = new byte[1024];
int len = 0;
// 代表当前读到的服务器数据的位置 ,同时这个值已经存储的文件的位置
int currentPostion = startposition;
while ((len = is.read(buffer)) != -1) {
if (stopflag) {// 如果点击了暂停下载,则直接跳出循环
return;
}
file.write(buffer, 0, len);
synchronized (MainActivity.this) {//累加total的时候,因为是多线程操作,必须加锁
total += len;
}
currentPostion += len;
// 需要把currentPostion 信息给持久化到存储设备
String position = currentPostion + "";
FileOutputStream fos = new FileOutputStream(postionfile);
fos.write(position.getBytes());
fos.flush();
fos.close();
}
file.close();
System.out.println("线程" + threadid + "下载完毕");
// 当线程下载完毕后 把文件删除掉
if (postionfile.exists()) {
postionfile.delete();
}
if (progressfile.exists()) {
progressfile.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
super.run();
}
}
public String getFilenName(String path) {
int start = path.lastIndexOf("/") + 1;
return path.substring(start, path.length());
}
@Override
protected void onDestroy() {
// 保存下载进度,并且保存文件的总长度,否则再次设置进度条的时候不知道总长度是不能进行设置值的
int progress = pb.getProgress();
int totalsize = pb.getMax();
if (progress < totalsize) {//如果是下载完成了,退出下载界面,不应该记录任何数据,下次进入应该重现开始下载
try {
FileOutputStream fos = new FileOutputStream(progressfile);
String progressstr = progress + "#" + totalsize;
fos.write(progressstr.getBytes());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
super.onDestroy();
}
}
}
以上就介绍了Android多线程下载及断点续传,包括了方面的内容,希望对Android开发有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_138733.html