android 多线程断点下载

使用多线程断点续传下载器在下载的时候多个线程并发可以占用服务器端更多资源,从而加快下载速度,在下载过程中记录每个线程已拷贝数据的数量,如果下载中断,比如无信号断线、电量不足等情况下,这就需要使用到断点续传功能,下次启动时从记录位置继续下载,可避免重复部分的下载。这里采用数据库来记录下载的进度。界面不是很好看,但功能都齐全。

android客户端与tomcat服务器端源码下载地址:http://download.csdn.net/detail/qqq01002013019/8238555


效果图:



android客户端代码:


下载器代码:

package cn.demo.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 android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import cn.demo.Dao.Dao;
import cn.demo.entity.DownloadInfo;
import cn.demo.entity.LoadInfo;
 
 public class Downloader {
 
     private String urlstr;   // 下载的地址   下载器标识    每个下载器有其具体信息(3条线程下载的具体信息)
     private String localfile;// 保存路径
     private int threadcount; // 线程数
     private Handler mHandler;// 消息处理器 
     private int fileSize;    // 所要下载的文件的大小
     private Context context; 
     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;      // 消息处理器                       
         this.context = context;                             
     }
     /**
      *判断是否正在下载 
      */
     public boolean isdownloading() {   
         return state == DOWNLOADING;    
     }
     
     /**
      * 得到downloader里的信息
      * 首先进行判断是否是第一次下载,如果是第一次就要进行初始化,并将下载器的信息保存到数据库中
      * 如果不是第一次下载,那就要从数据库中读出之前下载的信息(起始位置,结束为止,文件大小等),并将下载信息返回给下载器
      */
     
     public LoadInfo getDownloaderInfors() {            //进行数据库操作     返回下载器组成的信息
         if (isFirst(urlstr)) {      // 第一次下载
             Log.v("TAG", "isFirst");
             init();  //获得所需要下载文件的大小  并且创建本地文件
             int range = fileSize / threadcount;  //每条线程负责下载模块的大小  
             infos = new ArrayList<DownloadInfo>();        
             for (int i = 0; i < threadcount - 1; i++) {
            // 下载器id 下载点   结束点   完成度   文件网络地址 
                 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.getInstance(context).saveInfos(infos);
             
             //创建一个LoadInfo对象记载下载器的具体信息
             LoadInfo loadInfo = new LoadInfo(fileSize, 0, urlstr);  // 文件大小   完成度    下载器标识(url) 每个下载器有其具体信息
             return loadInfo;
             
         } else {
             //得到数据库中已有的urlstr的下载器的具体信息()
             infos = Dao.getInstance(context).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) {     
    //查看对应URL数据库中是否有数据 
         return Dao.getInstance(context).isHasInfors(urlstr);
     }
     
     /**
      *  利用线程开始下载数据
      */
     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.getInstance(context).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();
             }  
         }
     }
     
     //删除数据库中urlstr对应的下载器信息
     public void delete(String urlstr) {
    Dao.getInstance(context).delete(urlstr);
     }
     
     //设置暂停
     public void pause() {
         state = PAUSE;
     }
     
     //重置下载状态
     public void reset() {
         state = INIT;
     }   
 }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值