安卓断点续传核心代码DownloadTask
2017-02-20 13:06
225 查看
废话不多说。我们直接上代码。重点用注释已经写出来了。与传统的下载略有不同。
/**
* 下载文件线程
* 从服务器获取需要下载的文件大小
*/
public class DownLoadTask extends Thread {
private FileInfo info;
private SQLiteDatabase db;
private DbHelper helper;//数据库帮助类
private int finished = 0;//当前已下载完成的进度
private OnProgressListener listener;//进度回调监听
public DownLoadTask(FileInfo info, DbHelper helper, OnProgressListener listener) {
this.info = info;
this.helper = helper;
this.db = helper.getReadableDatabase();
this.listener = listener;
info.setDownLoading(true);
}
@Override
public void run() {
getLength();
HttpURLConnection connection = null;
RandomAccessFile rwd = null;
try {
URL url = new URL(info.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
//从上次下载完成的地方下载
int start = info.getFinished();
//设置下载位置(从服务器上取要下载文件的某一段)
connection.setRequestProperty("Range", "bytes=" + start + "-" + info.getLength());//设置下载范围
//设置文件写入位置
File file = new File(DownLoaderManger.FILE_PATH, info.getFileName());
rwd = new RandomAccessFile(file, "rwd");
//从文件的某一位置开始写入
rwd.seek(start);
finished += info.getFinished();
if (connection.getResponseCode() == 206) {//文件部分下载,返回码为206
InputStream is = connection.getInputStream();
byte[] buffer = new byte[1024 * 4];
int len;
while ((len = is.read(buffer)) != -1) {
//写入文件
rwd.write(buffer, 0, len);
finished += len;
info.setFinished(finished);
//更新界面显示
Message msg = new Message();
msg.what = 0x123;
msg.arg1 = info.getLength();
msg.arg2 = info.getFinished();
handler.sendMessage(msg);
//停止下载
if (info.isStop()) {
info.setDownLoading(false);
//保存此次下载的进度
helper.updateData(db, info);
db.close();
return;
}
}
//下载完成
info.setDownLoading(false);
helper.updateData(db, info);
db.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (rwd != null) {
rwd.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 首先开启一个线程去获取要下载文件的大小(长度)
*/
private void getLength() {
HttpURLConnection connection = null;
try {
//连接网络
URL url = new URL(info.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
int length = -1;
if (connection.getResponseCode() == 200) {//网络连接成功
//获得文件长度
length = connection.getContentLength();
}
if (length <= 0) {
//连接失败
return;
}
//创建文件保存路径
File dir = new File(DownLoaderManger.FILE_PATH);
if (!dir.exists()) {
dir.mkdirs();
}
info.setLength(length);
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (connection != null) {
connection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 更新进度
*/
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x123:
if (listener != null) {
listener.updateProgress(msg.arg1, msg.arg2);
}
break;
}
}
};
}
/**
* 下载文件线程
* 从服务器获取需要下载的文件大小
*/
public class DownLoadTask extends Thread {
private FileInfo info;
private SQLiteDatabase db;
private DbHelper helper;//数据库帮助类
private int finished = 0;//当前已下载完成的进度
private OnProgressListener listener;//进度回调监听
public DownLoadTask(FileInfo info, DbHelper helper, OnProgressListener listener) {
this.info = info;
this.helper = helper;
this.db = helper.getReadableDatabase();
this.listener = listener;
info.setDownLoading(true);
}
@Override
public void run() {
getLength();
HttpURLConnection connection = null;
RandomAccessFile rwd = null;
try {
URL url = new URL(info.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
//从上次下载完成的地方下载
int start = info.getFinished();
//设置下载位置(从服务器上取要下载文件的某一段)
connection.setRequestProperty("Range", "bytes=" + start + "-" + info.getLength());//设置下载范围
//设置文件写入位置
File file = new File(DownLoaderManger.FILE_PATH, info.getFileName());
rwd = new RandomAccessFile(file, "rwd");
//从文件的某一位置开始写入
rwd.seek(start);
finished += info.getFinished();
if (connection.getResponseCode() == 206) {//文件部分下载,返回码为206
InputStream is = connection.getInputStream();
byte[] buffer = new byte[1024 * 4];
int len;
while ((len = is.read(buffer)) != -1) {
//写入文件
rwd.write(buffer, 0, len);
finished += len;
info.setFinished(finished);
//更新界面显示
Message msg = new Message();
msg.what = 0x123;
msg.arg1 = info.getLength();
msg.arg2 = info.getFinished();
handler.sendMessage(msg);
//停止下载
if (info.isStop()) {
info.setDownLoading(false);
//保存此次下载的进度
helper.updateData(db, info);
db.close();
return;
}
}
//下载完成
info.setDownLoading(false);
helper.updateData(db, info);
db.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (rwd != null) {
rwd.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 首先开启一个线程去获取要下载文件的大小(长度)
*/
private void getLength() {
HttpURLConnection connection = null;
try {
//连接网络
URL url = new URL(info.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
int length = -1;
if (connection.getResponseCode() == 200) {//网络连接成功
//获得文件长度
length = connection.getContentLength();
}
if (length <= 0) {
//连接失败
return;
}
//创建文件保存路径
File dir = new File(DownLoaderManger.FILE_PATH);
if (!dir.exists()) {
dir.mkdirs();
}
info.setLength(length);
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (connection != null) {
connection.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 更新进度
*/
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x123:
if (listener != null) {
listener.updateProgress(msg.arg1, msg.arg2);
}
break;
}
}
};
}
相关文章推荐
- 使用系统类NSURLSessionDownloadTask实现断点续传(音乐视频下载等)
- 使用系统类NSURLSessionDownloadTask实现断点续传(音乐视频下载等)
- Android学习 - Android断点续传核心代码
- 使用系统类NSURLSessionDownloadTask实现断点续传(音乐视频下载等)
- 安卓开发-Sqlite创建数据库实例(核心代码讲解)
- ios 后台下载,app退出再进入可以断点续传 NSURLSessionDownloadTask(一)
- 使用系统类NSURLSessionDownloadTask实现断点续传(音乐视频下载等)
- NSURLSessionDownloadTask的深度断点续传
- android安卓触摸事件touchEvent分发的 核心精辟代码
- 安卓中多线程断点续传文件下载核心代码总结
- iOS_NSURLSessionDownloadTask_显示加载进度_断点续传(网络中断情况).
- 微软开放Windows CE全部核心代码
- (4)jBPM websale代码学习: task.jsp(jBPM的两种分支策略)
- 用.net实现远程获取其他网站页面内容!(核心代码分析)
- 生成静态文件的新闻系统核心代码(转)
- Download ABAP Spool to PDF(代码样例)
- 生成静态文件的新闻系统核心代码(.net C#)
- C# 关于断点续传的代码资料1
- 用.net实现远程获取其他网站页面内容!(核心代码分析)
- 生成静态文件的新闻系统核心代码(.net C#)