package com.icss.service;
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.icss.dao.Dao;
import com.icss.entity.DownloadInfo;
import com.icss.entity.LoadInfo;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
public class Downloader {
private String urlstr;// 下载的地址
private String localfile;// 保存路径
private int threadcount;// 线程数
private Handler mHandler;// 消息处理器
private Dao dao;// 工具类
private int fileSize;// 所要下载的文件的大小
private List<DownloadInfo> infos;// 存放下载信息类的集合
private static final int INIT = 1;// 定义三种下载的状态:初始化状态,正在下载状态,暂停状态
private static final int DOWNLOADING = 2;
private static final int PAUSE = 3;
private int state = INIT;
public Downloader(String urlstr, String localfile, int threadcount,
Context context, Handler mHandler) {
this.urlstr = urlstr;
this.localfile = localfile;
this.threadcount = threadcount;
this.mHandler = mHandler;
dao = new Dao(context);
}
/**
* 判断是否正在下载
*/
public boolean isdownloading() {
return state == DOWNLOADING;
}
/**
* 得到downloader里的信息 首先进行判断是否是第一次下载,如果是第一次就要进行初始化,并将下载器的信息保存到数据库中
* 如果不是第一次下载,那就要从数据库中读出之前下载的信息(起始位置,结束为止,文件大小等),并将下载信息返回给下载器
*/
public LoadInfo getDownloaderInfors() {
if (isFirst(urlstr)) {
init();
int range = fileSize / threadcount;
infos = new ArrayList<DownloadInfo>();
for (int i = 0; i < threadcount - 1; i++) {
DownloadInfo info = new DownloadInfo(i, i * range, (i + 1)
* range - 1, 0, urlstr);
infos.add(info);
}
DownloadInfo info = new DownloadInfo(threadcount - 1,
(threadcount - 1) * range, fileSize - 1, 0, urlstr);
infos.add(info);
// 保存infos中的数据到数据库
dao.saveInfos(infos);
// 创建一个LoadInfo对象记载下载器的具体信息
LoadInfo loadInfo = new LoadInfo(fileSize, 0, urlstr);
return loadInfo;
} else {
// 得到数据库中已有的urlstr的下载器的具体信息
infos = dao.getInfos(urlstr);
Log.v("TAG", "not isFirst size=" + infos.size());
int size = 0;
int compeleteSize = 0;
for (DownloadInfo info : infos) {
compeleteSize += info.getCompeleteSize();
size += info.getEndPos() - info.getStartPos() + 1;
}
return new LoadInfo(size, compeleteSize, urlstr);
}
}
/**
*/
private void init() {
try {
URL url = new URL(urlstr);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
fileSize = connection.getContentLength();
File file = new File(localfile);
if (!file.exists()) {
file.createNewFile();
}
// 本地访问文件
RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
accessFile.setLength(fileSize);
accessFile.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 判断是否是第一次 下载
*/
private boolean isFirst(String urlstr) {
return dao.isHasInfors(urlstr);
}
/**
* 114 * 利用线程开始下载数据 115
*/
public void download() {
if (infos != null) {
if (state == DOWNLOADING)
return;
state = DOWNLOADING;
for (DownloadInfo info : infos) {
new MyThread(info.getThreadId(), info.getStartPos(),
info.getEndPos(), info.getCompeleteSize(),
info.getUrl()).start();
}
}
}
public class MyThread extends Thread {
private int threadId;
private int startPos;
private int endPos;
private int compeleteSize;
private String urlstr;
public MyThread(int threadId, int startPos, int endPos,
int compeleteSize, String urlstr) {
this.threadId = threadId;
this.startPos = startPos;
this.endPos = endPos;
this.compeleteSize = compeleteSize;
this.urlstr = urlstr;
}
@Override
public void run() {
HttpURLConnection connection = null;
RandomAccessFile randomAccessFile = null;
InputStream is = null;
try {
URL url = new URL(urlstr);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
// 设置范围,格式为Range:bytes x-y;
connection.setRequestProperty("Range", "bytes="
+ (startPos + compeleteSize) + "-" + endPos);
randomAccessFile = new RandomAccessFile(localfile, "rwd");
randomAccessFile.seek(startPos + compeleteSize);
// 将要下载的文件写到保存在保存路径下的文件中
is = connection.getInputStream();
byte[] buffer = new byte[4096];
int length = -1;
while ((length = is.read(buffer)) != -1) {
randomAccessFile.write(buffer, 0, length);
compeleteSize += length;
// 更新数据库中的下载信息
dao.updataInfos(threadId, compeleteSize, urlstr);
// 用消息将下载信息传给进度条,对进度条进行更新
Message message = Message.obtain();
message.what = 1;
message.obj = urlstr;
message.arg1 = length;
mHandler.sendMessage(message);
if (state == PAUSE) {
return;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
randomAccessFile.close();
connection.disconnect();
dao.closeDb();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// 删除数据库中urlstr对应的下载器信息
public void delete(String urlstr) {
dao.delete(urlstr);
}
// 设置暂停
public void pause() {
state = PAUSE;
}
// 重置下载状态
public void reset() {
state = INIT;
}
}
android 多任务断点下载 DEMO
4星 · 超过85%的资源 需积分: 9 117 浏览量
2011-11-04
20:42:11
上传
评论 3
收藏 88KB ZIP 举报
TT5267621
- 粉丝: 154
- 资源: 41
最新资源
- onenet_chongwukanhu_v06.apk
- 2022NOC软件创意编程赛项真题C++初中-决赛(有解析)
- 2022NOC软件创意编程赛项真题python初中-决赛(有解析)
- openLayer-本地数据加载 (day5)
- A题-正弦信号发生器.xdf
- 2022NOC软件创意编程赛项真题python小学高年级-决赛(有解析)
- mathml转换latex需要的xsl文件
- 2022NOC软件创意编程赛项真题图形化小学高年级-决赛(有解析)
- gbase驱动下载gbase-connector-java-8.3.81.53驱动下载
- 2022NOC软件创意编程赛项真题图形化小学低年级-决赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页