Android实现断点下载的方法

最近做的项目中需要实现断点下载,即用户一次下载可以分多次进行,下载过程可以中断,在目前大多数的带离线缓存的软件都是需要实现这一功能。本文阐述了通过sqlite3简单实现了一个具有断点下载功能的demo。言归正传,开始正文。

设计

数据库表存储元数据
DBHelper.java
用于业务存储的Dao
Dao.java
抽象下载信息的Bean
LoadInfo.java
呈现下载信息View
MainActivity.java
存储下载信息Bean
DownloadInfo.java
封装好的下载类
Downloader.java

代码结构

具体实现

下载信息类:DownloadInfo.java

这里的代码很简单,就是一个用来保存下载信息的类,很简单,没啥好说的,具体看注释

package entity;
public class DownloadInfo {

  private int threadId;//线程ID

  private int startPos;//下载起始位置

  private int endPos;//下载结束位置

  private int completeSize;//下载完成量

  private String url;//资源URL

  public DownloadInfo(int tId,int sp, int ep,int cSize,String address){
    threadId=tId;
    startPos=sp;
    endPos=ep;
    completeSize = cSize;
    url=address;
  }

  /**
   * @return the threadId
   */
  public int getThreadId() {
    return threadId;
  }

  /**
   * @param threadId the threadId to set
   */
  public void setThreadId(int threadId) {
    this.threadId = threadId;
  }

  /**
   * @return the startPos
   */
  public int getStartPos() {
    return startPos;
  }

  /**
   * @param startPos the startPos to set
   */
  public void setStartPos(int startPos) {
    this.startPos = startPos;
  }

  /**
   * @return the endPos
   */
  public int getEndPos() {
    return endPos;
  }

  /**
   * @param endPos the endPos to set
   */
  public void setEndPos(int endPos) {
    this.endPos = endPos;
  }

  /**
   * @return the completeSize
   */
  public int getCompleteSize() {
    return completeSize;
  }

  /**
   * @param completeSize the completeSize to set
   */
  public void setCompleteSize(int completeSize) {
    this.completeSize = completeSize;
  }

  /**
   * @return the url
   */
  public String getUrl() {
    return url;
  }

  /**
   * @param url the url to set
   */
  public void setUrl(String url) {
    this.url = url;
  }

  @Override
  public String toString() {
    // TODO Auto-generated method stub
    return "threadId:"+threadId+",startPos:"+startPos+",endPos:"+endPos+",completeSize:"+completeSize+",url:"+url;
  }
}

数据库 DBHelper.java

package db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

  String sql ="create table download_info (id integer PRIMARY KEY AUTOINCREMENT,"
      + "thread_id integer,"
      + "start_pos integer,"
      + "end_pos integer,"
      + "complete_size integer,"
      + "url char)";

  public DBHelper(Context context) {
    // TODO Auto-generated constructor stub
    super(context, "download.db", null, 1);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(sql);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

  }

}

数据库业务管理类 Dao.java

package db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import entity.DownloadInfo;

public class Dao {

  private DBHelper dbHelper;

  public Dao(Context context){
    dbHelper = new DBHelper(context);
  }

  public boolean isNewTask(String url){
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    String sql = "select count(*) from download_info where url=?";
    Cursor cursor = db.rawQuery(sql, new String[]{url});
    cursor.moveToFirst();
    int count = cursor.getInt(0);
    cursor.close();
    return count == 0;
  }

  public void saveInfo(List<DownloadInfo> infos){
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String sql = "insert into download_info(thread_id,start_pos,end_pos,complete_size,url) values(?,?,?,?,?)";
    Object[] bindArgs= null;
    for(DownloadInfo info:infos){
      bindArgs=new Object[]{info.getThreadId(),info.getStartPos(),info.getEndPos(),info.getCompleteSize(),info.getUrl()};
      db.execSQL(sql, bindArgs);
    }

  }
  public List<DownloadInfo> getInfo(String url){
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    List<DownloadInfo> infos = new ArrayList<DownloadInfo>();
    String sql ="select thread_id,start_pos,end_pos,complete_size,url from download_info where url=?";
    Cursor cursor=db.rawQuery(sql, new String[]{url});
    while(cursor.moveToNext()){
      DownloadInfo info = new DownloadInfo(cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), cursor.getInt(3),cursor.getString(4));
      infos.add(info);
    }
    cursor.close();
    return infos;

  }

  public void deleteInfo(String url){
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    db.delete("download_info", "url=?", new String[]{url});
    db.close();
  }

  public void updateInfo(int completeSize,int threadId,String url){
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String sql ="update download_info set complete_size=? where thread_id=? and url=?";
    db.execSQL(sql, new Object[]{completeSize,threadId,url});
  }

  public void closeDB(){
    dbHelper.close();
  }

}

当前状态保存类 LoadInfo.java

package entity;

public class LoadInfo {
  private int fileSize;
  private int completeSize;
  private String url;
  public LoadInfo(int fs,int cSize,String address){
    fileSize=fs;
    completeSize = cSize;
    url=address;
  }
  /**
   * @return the fileSize
   */
  public int getFileSize() {
    return fileSize;
  }
  /**
   * @param fileSize the fileSize to set
   */
  public void setFileSize(int fileSize) {
    this.fileSize = fileSize;
  }
  /**
   * @return the completeSize
   */
  public int getCompleteSize() {
    return completeSize;
  }

  /**
   * @param completeSize the completeSize to set
   */
  public void setCompleteSize(int completeSize) {
    this.completeSize = completeSize;
  }

  /**
   * @return the url
   */
  public String getUrl() {
    return url;
  }

  /**
   * @param url the url to set
   */
  public void setUrl(String url) {
    this.url = url;
  }
}

下载助手类:Downloader.java

package com.winton.downloadmanager;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
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 db.Dao;
import entity.DownloadInfo;
import entity.LoadInfo;

public class Downloader {
  private String url;
  private String localPath;
  private int threadCount;
  private Handler mHanler;
  private Dao dao;
  private int fileSize;

  private List<DownloadInfo> infos;

  private final static int INIT = 1;

  private final static int DOWNLOADING =2;

  private final static int PAUSE =3;

  private int state = INIT;

  public Downloader(String address,String lPath,int thCount,Context context,Handler handler){
    url =address;
    localPath = lPath;
    threadCount = thCount;
    mHanler = handler;
    dao = new Dao(context);
  }

  public boolean isDownloading(){
    return state == DOWNLOADING;
  }

  public LoadInfo getDownLoadInfos(){
    if(isFirstDownload(url)){
      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, url);
        infos.add(info);
      }
      dao.saveInfo(infos);
      return new LoadInfo(fileSize, 0, url);
    }else{
      infos = dao.getInfo(url);
      int size = 0;
      int completeSize =0;
      for(DownloadInfo info:infos){
        completeSize += info.getCompleteSize();
        size += info.getEndPos()-info.getStartPos()+1;
      }
      return new LoadInfo(size, completeSize, url);
    }
  }
  public boolean isFirstDownload(String url){
    return dao.isNewTask(url);
  }

  public void init(){
    try {
      URL mUrl = new URL(this.url);
      HttpURLConnection connection = (HttpURLConnection)mUrl.openConnection();
      connection.setConnectTimeout(5000);
      connection.setRequestMethod("GET");
      fileSize = connection.getContentLength();

      File file = new File(localPath);
      if(!file.exists()){
        file.createNewFile();
      }
      RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");
      accessFile.setLength(fileSize);
      accessFile.close();
      connection.disconnect();

    } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void download(){
    if(infos != null){
      if(state ==DOWNLOADING){
        return;
      }
      state = DOWNLOADING;
      for(DownloadInfo info:infos){
        new DownloadThread(info.getThreadId(), info.getStartPos(), info.getEndPos(), info.getCompleteSize(), info.getUrl()).start();

      }
    }
  }

  class DownloadThread extends Thread{

    private int threadId;
    private int startPos;
    private int endPos;
    private int completeSize;
    private String url;

    public DownloadThread(int tId,int sp,int ep,int cSize,String address) {
      // TODO Auto-generated constructor stub
      threadId=tId;
      startPos=sp;
      endPos = ep;
      completeSize = cSize;
      url = address;
    }

    @Override
    public void run() {
      // TODO Auto-generated method stub
      HttpURLConnection connection = null;
      RandomAccessFile randomAccessFile = null;
      InputStream is = null;

      try {
        URL mUrl = new URL(url);
        connection = (HttpURLConnection)mUrl.openConnection();
        connection.setConnectTimeout(5000);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Range", "bytes="+(startPos+completeSize)+"-"+endPos);
        randomAccessFile = new RandomAccessFile(localPath, "rwd");
        randomAccessFile.seek(startPos+completeSize);
        is=connection.getInputStream();
        byte[] buffer = new byte[4096];
        int length =-1;
        while((length=is.read(buffer)) != -1){
          randomAccessFile.write(buffer, 0, length);
          completeSize +=length;
          dao.updateInfo(threadId, completeSize, url);
          Message msg = Message.obtain();
          msg.what=1;
          msg.obj=url;
          msg.arg1=length;
          mHanler.sendMessage(msg);
          if(state==PAUSE){
            return;
          }
        }
      } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }finally{
        try {
          is.close();
          randomAccessFile.close();
          connection.disconnect();
          dao.closeDB();
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }

      }

    }

  }
  public void delete(String url){
    dao.deleteInfo(url);
  }
  public void reset(){
    state=INIT;
  }
  public void pause(){
    state=PAUSE;
  }

}

View呈现类:MainActivity.java

package com.winton.downloadmanager;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import entity.LoadInfo;

public class MainActivity extends Activity implements OnClickListener{
  private TextView name;

  private ProgressBar process;

  private Button start,stop;
  private Downloader downloader;

  //处理下载进度UI的
  Handler handler = new Handler(){
    public void handleMessage(android.os.Message msg) {
      if(msg.what==1){
        name.setText((String)msg.obj);
        int lenght = msg.arg1;
        process.incrementProgressBy(lenght);
      }
      if(msg.what==2){
        int max =msg.arg1;
        process.setMax(max);
        Toast.makeText(getApplicationContext(), max+"", 1).show();
      }
    };
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name=(TextView)findViewById(R.id.tv_name);
    process=(ProgressBar)findViewById(R.id.pb_download);
    start = (Button)findViewById(R.id.bt_start);
    stop = (Button)findViewById(R.id.bt_stop);
    start.setOnClickListener(this);
    stop.setOnClickListener(this);
    downloader=new Downloader("http://img4.duitang.com/uploads/item/201206/11/20120611174542_5KRMj.jpeg", Environment.getExternalStorageDirectory().getPath()+"/test1.jpg", 1, getApplicationContext(), handler);
  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v==start){
      new Thread(new Runnable() {

        @Override
        public void run() {
          // TODO Auto-generated method stub
          LoadInfo loadInfo = downloader.getDownLoadInfos();
          Message msg =handler.obtainMessage();
          msg.what=2;
          msg.arg1=loadInfo.getFileSize();
          handler.sendMessage(msg);
          downloader.download();
        }
      }).start();
      return;
    }
    if(v==stop){
      downloader.pause();
      return;
    }
  }
}

运行效果

总体比较简单,基本实现了 断点下载的功能,而且开启了多个线程去实现分块下载,对初学的同学有一定的帮助。

这些代码我也是从网上各种搜集而来,自己亲自动手敲了一遍,并做了一些小的改动,对整个断点下载的过程有了一个深刻的认识。因此平时要多敲代码,善于总结,必能有所收获。

(0)

相关推荐

  • Android通过HttpURLConnection和HttpClient接口实现网络编程

    Android中提供的HttpURLConnection和HttpClient接口可以用来开发HTTP程序.以下是学习中的一些经验. 1.HttpURLConnection接口 首先需要明确的是,Http通信中的POST和GET请求方式的不同.GET可以获得静态页面,也可以把参数放在URL字符串后面,传递给服务器.而POST方法的参数是放在Http请求中.因此,在编程之前,应当首先明确使用的请求方法,然后再根据所使用的方式选择相应的编程方式.HttpURLConnection是继承于URLCon

  • Golang+Android基于HttpURLConnection实现的文件上传功能示例

    本文实例讲述了Golang+Android基于HttpURLConnection实现的文件上传功能.分享给大家供大家参考,具体如下: 这里要演示的是使用Android程序作为客户端(使用HttpURLConnection访问网络),Golang程序作为服务器端,实现文件上传. 客户端代码: public static String uploadFile(String uploadUrl, String filePath) { Log.v(TAG, "url:" + uploadUrl)

  • Android HttpURLConnection.getResponseCode()错误解决方法

    导语:个人对网络连接接触的不多,在使用时自己发现一些问题,记录一下. 正文:我在使用HttpURLConnection.getResponseCode()的时候直接报错是IOException错误,responseCode = -1.一直想不明白,同一个程序我调用了两次,结果有一个链接一直OK,另一个却一直报这个错误.后来发现两个链接的区别,有一个返回的内容是空的,所以导致了这个错误. 解决方法: 方法1.网页返回内容不能是空: 方法2.不要用这个接口咯.

  • Android HttpURLConnection断点下载(单线程)

    HttpCilent 跟 HttpURLConnection 是安卓原生的用来实现http请求的类: Android 6.0之后取消了HttpClient,不支持跟新 ,今天小编使用的是HttpURLConnection : 直接上代码: URL url = null; BufferedInputStream bin = null; HttpURLConnection httpURLConnection = null; Context context; try { //你要下载文件的路径 Str

  • Android 中HttpURLConnection与HttpClient使用的简单实例

    1:HttpHelper.java 复制代码 代码如下: public class HttpHelper {    //1:标准的Java接口    public static String getStringFromNet1(String param){        String result="";        try{            URL url=new URL(param);            HttpURLConnection conn=(HttpURLCo

  • Android程序开发通过HttpURLConnection上传文件到服务器

    一:实现原理 最近在做Android客户端的应用开发,涉及到要把图片上传到后台服务器中,自己选择了做Spring3 MVC HTTP API作为后台上传接口,android客户端我选择用HttpURLConnection来通过form提交文件数据实现上传功能,本来想网上搜搜拷贝一下改改代码就好啦,发现根本没有现成的例子,多数的例子都是基于HttpClient的或者是基于Base64编码以后作为字符串来传输图像数据,于是我不得不自己动手,参考了网上一些资料,最终实现基于HttpURLConnect

  • Android中使用HttpURLConnection实现GET POST JSON数据与下载图片

    Android6.0中把Apache HTTP Client所有的包与类都标记为deprecated不再建议使用所有跟HTTP相关的数据请求与提交操作都通过HttpURLConnection类实现,现实是很多Android开发者一直都Apache HTTP Client来做andoird客户端与后台HTTP接口数据交互,小编刚刚用HttpURLConnection做了一个android的APP,不小心踩到了几个坑,总结下最常用的就通过HttpURLConnection来POST提交JSON数据与

  • Android使用多线程实现断点下载

    多线程下载是加快下载速度的一种方式,通过开启多个线程去执行一个任务..可以使任务的执行速度变快..多线程的任务下载时常都会使用得到..比如说我们手机内部应用宝的下载机制..一定是通过使用了多线程创建的下载器..并且这个下载器可以实现断点下载..在任务被强行终止之后..下次可以通过触发按钮来完成断点下载...那么如何实现断点下载这就是一个问题了.. 首先我们需要明确一点就是多线程下载器通过使用多个线程对同一个任务进行下载..但是这个多线程并不是线程的数目越多,下载的速度就越快..当线程增加的很多的

  • Android实现多线程断点下载的方法

    本文实例讲述了Android实现多线程断点下载的方法.分享给大家供大家参考.具体实现方法如下: package cn.itcast.download; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputSt

  • Android中HttpURLConnection与HttpClient的使用与封装

    1.写在前面 大部分andriod应用需要与服务器进行数据交互,HTTP.FTP.SMTP或者是直接基于SOCKET编程都可以进行数据交互,但是HTTP必然是使用最广泛的协议.     本文并不针对HTTP协议的具体内容,仅探讨android开发中使用HTTP协议访问网络的两种方式--HttpURLConnection和HttpClient     因为需要访问网络,需在AndroidManifest.xml中添加如下权限 <uses-permission android:name="an

随机推荐