安卓断点续传核心代码DownloadTask
来源:互联网 发布:淘宝网蜜袋鼯购买 编辑:程序博客网 时间:2024/05/16 01:47
废话不多说。我们直接上代码。重点用注释已经写出来了。与传统的下载略有不同。
/** * 下载文件线程 * 从服务器获取需要下载的文件大小 */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; } } };}
0 0
- 安卓断点续传核心代码DownloadTask
- Android断点续传核心代码
- Android断点续传核心代码
- 断点续传核心代码
- Android断点续传核心代码
- 断点续传核心代码
- C# 单线程断点续传下载核心代码
- Android学习 - Android断点续传核心代码
- 安卓断点续传转载文章
- 安卓--实现多线程断点续传
- 安卓断点续传支持安卓6.0
- 安卓中多线程断点续传文件下载核心代码总结
- 安卓核心基础
- 安卓断点续传程序示例下载
- 安卓源代码核心结构
- android安卓触摸事件touchEvent分发的 核心精辟代码
- 安卓开发-Sqlite创建数据库实例(核心代码讲解)
- 安卓文件下载之断点续传(一)
- Arduino简单实例之四_PS2游戏摇杆
- centos 与hadoop2.7.3环境下hive2.1.1安装详解
- 如何用github上传代码
- UVa129
- UML用户指南第二版-读书笔记-第2章-UML介绍
- 安卓断点续传核心代码DownloadTask
- POJ 1163 The Triangle
- USB2.0协议分析
- 菜鸟读文献系列(二)
- JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序
- Python学习的个人笔记(基础语法)
- Tensorflow建立与读取TFrecorder文件
- 每天一节组合数学
- web服务器apache架构与原理