android 多线程断点续传下载 二

文章出处
  在上一集中,我们简单介绍了如何创建多任务下载,但那种还不能拿来实用,这一集我们重点通过代码为大家展示如何创建多线程断点续传下载,这在实际项目中很常用.

main.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
    <EditText  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/editText"  
        android:text="http://gongxue.cn/yingyinkuaiche/UploadFiles_9323/201008/2010082909434077.mp3"  
    />  
    <LinearLayout  
        android:orientation="horizontal"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
    >  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:id="@+id/downButton"  
            android:text="Download"  
        />  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:id="@+id/pauseButton"  
            android:enabled="false"  
            android:text="Pause"  
        />  
    </LinearLayout>  
      
    <ProgressBar  
        android:layout_width="match_parent"  
        android:layout_height="18dp"  
        style="?android:attr/progressBarStyleHorizontal"  
        android:id="@+id/progressBar"  
    />  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:id="@+id/textView"  
        android:gravity="center"  
    />  
</LinearLayout>  

String.xml

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <string name="hello">Hello World, Main!</string>  
    <string name="app_name">多线程断点续传下载</string>  
</resources>  

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="sms.multithreaddownload"  
      android:versionCode="1"  
      android:versionName="1.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".Main"  
                  android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
     <uses-library android:name="android.test.runner" />  
    </application>  
    <uses-sdk android:minSdkVersion="8" />  
    <instrumentation  
        android:targetPackage="sms.multithreaddownload"  
        android:name="android.test.InstrumentationTestRunner" />  
    <!-- 访问网络的权限 -->  
    <uses-permission android:name="android.permission.INTERNET"/>  
    <!-- SDCard写数据的权限 -->  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
</manifest>   

activity程序:

package sms.multithreaddownload;

import java.io.File;

import sms.multithreaddownload.bean.DownloadListener;
import sms.multithreaddownload.service.DownloadService;
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.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity {
    private EditText path;
    private TextView progress;
    private ProgressBar progressBar;
    private Handler handler = new UIHandler();
    private DownloadService servcie;
    private Button downButton;
    private Button pauseButton;

    private final class UIHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    int downloaded_size = msg.getData().getInt("size");
                    progressBar.setProgress(downloaded_size);
                    int result = (int) ((float) downloaded_size / progressBar.getMax() * 100);
                    progress.setText(result + "%");
                    if (progressBar.getMax() == progressBar.getProgress()) {
                        Toast.makeText(getApplicationContext(), "下载完成", Toast.LENGTH_LONG).show();
                    }
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        path = (EditText) this.findViewById(R.id.editText);
        progress = (TextView) this.findViewById(R.id.textView);
        progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
        downButton = (Button) this.findViewById(R.id.downButton);
        pauseButton = (Button) this.findViewById(R.id.pauseButton);
        downButton.setOnClickListener(new DownloadButton());
        pauseButton.setOnClickListener(new PauseButton());
    }

    private final class DownloadButton implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            DownloadTask task;
            try {
                task = new DownloadTask(path.getText().toString());
                servcie.isPause = false;
                v.setEnabled(false);
                pauseButton.setEnabled(true);
                new Thread(task).start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public class PauseButton implements OnClickListener {
        @Override
        public void onClick(View v) {
            servcie.isPause = true;
            v.setEnabled(false);
            downButton.setEnabled(true);
        }
    }

    public void pause(View v) {
    }

    private final class DownloadTask implements Runnable {

        public DownloadTask(String target) throws Exception {
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                File destination = Environment.getExternalStorageDirectory();
                servcie = new DownloadService(target, destination, 3, getApplicationContext());
                progressBar.setMax(servcie.fileSize);
            } else {
                Toast.makeText(getApplicationContext(), "SD卡不存在或写保护!", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void run() {
            try {
                servcie.download(new DownloadListener() {

                    @Override
                    public void onDownload(int downloaded_size) {
                        Message message = new Message();
                        message.what = 1;
                        message.getData().putInt("size", downloaded_size);
                        handler.sendMessage(message);
                    }

                });
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}

工具类:

package sms.multithreaddownload.bean;

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

public class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, "MultiDownLoad.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE fileDownloading(_id integer primary key autoincrement,downPath varchar(100),threadId INTEGER,downLength INTEGER)");
    }

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

    }

}
package sms.multithreaddownload.bean;

public interface DownloadListener {
    public void onDownload(int downloaded_size);
}
package sms.multithreaddownload.bean;  
  
import java.io.File;  
import java.io.InputStream;  
import java.io.RandomAccessFile;  
import java.net.HttpURLConnection;  
import java.net.URL;  
  
import sms.multithreaddownload.service.DownloadService;  
  
import android.util.Log;  
  
public final class MultiThreadDownload implements Runnable {  
    public int id;  
    private RandomAccessFile savedFile;  
    private String path;  
    /* 当前已下载量 */  
    public int currentDownloadSize = 0;  
    /* 下载状态 */  
    public boolean finished;  
    /* 用于监视下载状态 */  
    private final DownloadService downloadService;  
    /* 线程下载任务的起始点 */  
    public int start;  
    /* 线程下载任务的结束点 */  
    private int end;  
  
    public MultiThreadDownload(int id, File savedFile, int block, String path, Integer downlength, DownloadService downloadService) throws Exception {  
        this.id = id;  
        this.path = path;  
        if (downlength != null) this.currentDownloadSize = downlength;  
        this.savedFile = new RandomAccessFile(savedFile, "rwd");  
        this.downloadService = downloadService;  
        start = id * block + currentDownloadSize;  
        end = (id + 1) * block;  
    }  
  
    @Override  
    public void run() {  
        try {  
            HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();  
            conn.setConnectTimeout(5000);  
            conn.setRequestMethod("GET");  
            conn.setRequestProperty("Range", "bytes=" + start + "-" + end); // 设置获取数据的范围  
  
            InputStream in = conn.getInputStream();  
            byte[] buffer = new byte[1024];  
            int len = 0;  
            savedFile.seek(start);  
            while (!downloadService.isPause && (len = in.read(buffer)) != -1) {  
                savedFile.write(buffer, 0, len);  
                currentDownloadSize += len;  
            }  
            savedFile.close();  
            in.close();  
            conn.disconnect();  
            if (!downloadService.isPause) Log.i(DownloadService.TAG, "Thread " + (this.id + 1) + "finished");  
            finished = true;  
        } catch (Exception e) {  
            e.printStackTrace();  
            throw new RuntimeException("File downloading error!");  
        }  
    }  
}  

service类:

package sms.multithreaddownload.service;  
  
import java.io.File;  
import java.io.RandomAccessFile;  
import java.net.HttpURLConnection;  
import java.net.URL;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
import java.util.Map.Entry;  
import java.util.UUID;  
import java.util.concurrent.ConcurrentHashMap;  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
  
import sms.multithreaddownload.bean.DBHelper;  
import sms.multithreaddownload.bean.DownloadListener;  
import sms.multithreaddownload.bean.MultiThreadDownload;  
  
import android.content.Context;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
  
  
public class DownloadService {  
    public static final String TAG = "tag";  
    /* 用于查询数据库 */  
    private DBHelper dbHelper;  
    /* 要下载的文件大小 */  
    public int fileSize;  
    /* 每条线程需要下载的数据量 */  
    private int block;  
    /* 保存文件地目录 */  
    private File savedFile;  
    /* 下载地址 */  
    private String path;  
    /* 是否停止下载 */  
    public boolean isPause;  
    /* 线程数 */  
    private MultiThreadDownload[] threads;  
    /* 各线程已经下载的数据量 */  
    private Map<Integer, Integer> downloadedLength = new ConcurrentHashMap<Integer, Integer>();  
  
    public DownloadService(String target, File destination, int thread_size, Context context) throws Exception {  
        dbHelper = new DBHelper(context);  
        this.threads = new MultiThreadDownload[thread_size];  
        this.path = target;  
        URL url = new URL(target);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setConnectTimeout(5000);  
        conn.setRequestMethod("GET");  
  
        if (conn.getResponseCode() != 200) {  
            throw new RuntimeException("server no response!");  
        }  
  
        fileSize = conn.getContentLength();  
        if (fileSize <= 0) {  
            throw new RuntimeException("file is incorrect!");  
        }  
        String fileName = getFileName(conn);  
        if (!destination.exists()) destination.mkdirs();  
        // 构建一个同样大小的文件  
        this.savedFile = new File(destination, fileName);  
        RandomAccessFile doOut = new RandomAccessFile(savedFile, "rwd");  
        doOut.setLength(fileSize);  
        doOut.close();  
        conn.disconnect();  
  
        // 计算每条线程需要下载的数据长度  
        this.block = fileSize % thread_size == 0 ? fileSize / thread_size : fileSize / thread_size + 1;  
        // 查询已经下载的记录  
        downloadedLength = this.getDownloadedLength(path);  
    }  
  
    private Map<Integer, Integer> getDownloadedLength(String path) {  
        SQLiteDatabase db = dbHelper.getReadableDatabase();  
        String sql = "SELECT threadId,downLength FROM fileDownloading WHERE downPath=?";  
        Cursor cursor = db.rawQuery(sql, new String[] { path });  
        Map<Integer, Integer> data = new HashMap<Integer, Integer>();  
        while (cursor.moveToNext()) {  
            data.put(cursor.getInt(0), cursor.getInt(1));  
        }  
        db.close();  
        return data;  
    }  
  
    private String getFileName(HttpURLConnection conn) {  
        String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());  
        if (fileName == null || "".equals(fileName.trim())) {  
            String content_disposition = null;  
            for (Entry<String, List<String>> entry : conn.getHeaderFields().entrySet()) {  
                if ("content-disposition".equalsIgnoreCase(entry.getKey())) {  
                    content_disposition = entry.getValue().toString();  
                }  
            }  
            try {  
                Matcher matcher = Pattern.compile(".*filename=(.*)").matcher(content_disposition);  
                if (matcher.find()) fileName = matcher.group(1);  
            } catch (Exception e) {  
                fileName = UUID.randomUUID().toString() + ".tmp"; // 默认名  
            }  
        }  
        return fileName;  
    }  
  
    public void download(DownloadListener listener) throws Exception {  
        this.deleteDownloading(); // 先删除上次的记录,再重新添加  
        for (int i = 0; i < threads.length; i++) {  
            threads[i] = new MultiThreadDownload(i, savedFile, block, path, downloadedLength.get(i), this);  
            new Thread(threads[i]).start();  
        }  
        this.saveDownloading(threads);  
  
        while (!isFinish(threads)) {  
            Thread.sleep(900);  
            if (listener != null) listener.onDownload(getDownloadedSize(threads));  
            this.updateDownloading(threads);  
        }  
        if (!this.isPause) this.deleteDownloading();// 完成下载之后删除本次下载记录  
    }  
  
    private void saveDownloading(MultiThreadDownload[] threads) {  
        SQLiteDatabase db = dbHelper.getWritableDatabase();  
        try {  
            db.beginTransaction();  
            for (MultiThreadDownload thread : threads) {  
                String sql = "INSERT INTO fileDownloading(downPath,threadId,downLength) values(?,?,?)";  
                db.execSQL(sql, new Object[] { path, thread.id, 0 });  
            }  
            db.setTransactionSuccessful();  
        } finally {  
            db.endTransaction();  
            db.close();  
        }  
    }  
  
    private void deleteDownloading() {  
        SQLiteDatabase db = dbHelper.getWritableDatabase();  
        String sql = "DELETE FROM fileDownloading WHERE downPath=?";  
        db.execSQL(sql, new Object[] { path });  
        db.close();  
    }  
  
    private void updateDownloading(MultiThreadDownload[] threads) {  
        SQLiteDatabase db = dbHelper.getWritableDatabase();  
        try {  
            db.beginTransaction();  
            for (MultiThreadDownload thread : threads) {  
                String sql = "UPDATE fileDownloading SET downLength=? WHERE threadId=? AND downPath=?";  
                db.execSQL(sql, new String[] { thread.currentDownloadSize + "", thread.id + "", path });  
            }  
            db.setTransactionSuccessful();  
        } finally {  
            db.endTransaction();  
            db.close();  
        }  
    }  
  
    private int getDownloadedSize(MultiThreadDownload[] threads) {  
        int sum = 0;  
        for (int len = threads.length, i = 0; i < len; i++) {  
            sum += threads[i].currentDownloadSize;  
        }  
        return sum;  
    }  
  
    private boolean isFinish(MultiThreadDownload[] threads) {  
        try {  
            for (int len = threads.length, i = 0; i < len; i++) {  
                if (!threads[i].finished) {  
                    return false;  
                }  
            }  
            return true;  
        } catch (Exception e) {  
            return false;  
        }  
    }  
}  

运行效果:


源码下载地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,383评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,522评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,852评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,621评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,741评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,929评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,076评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,803评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,265评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,582评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,716评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,395评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,039评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,027评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,488评论 2 361
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,612评论 2 350

推荐阅读更多精彩内容