Android 学习之--android多线程断点下载

我们平时都用"迅雷"下载软件,当下载到一半的时候突然断网,下次开启的时候能够从上次下载的地方继续下载,而且下载速度很快,那么这是怎么做到的呢!

其实它的“快”其实就是多线程的下载实现的,断点下载的原理是将每次下载的字节数存取下来,保证存取的子节点跟下载的同步,并在用户下次下载的时候自动读取

存储点,并以存储点为开始值继续下载。那么android里面如何实现这么断点的下载呢?

在res的布局文件里面先画一个带有进度条的下载界面

 

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.thread.MainActivity" >

    <EditText
        android:id="@+id/et_path"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入下载路径"
        android:text="下载文件" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="downLoad"
        android:text="下载" />

    <ProgressBar
        android:id="@+id/pd"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="下载进度" />

</LinearLayout>

 

package com.example.thread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static EditText et_path;
    private static ProgressBar pb;
    private static TextView tv;
    public static int runningThread = 3;
    protected static final int DOWN_LOAD_ERROR = 1;
    protected static final int SERVER_ERROR = 2;
    protected static final int DOWN_LOAD_FINSIH = 3;
    protected static final int SHOW_TEXT = 4;
    public static int currentProcess = 0;// 当前进度
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case DOWN_LOAD_ERROR:
                Toast.makeText(getApplicationContext(), "下载失败", 0).show();
                break;
            case SERVER_ERROR:
                Toast.makeText(getApplicationContext(), "服务器异常", 0).show();
                break;
            case DOWN_LOAD_FINSIH:
                Toast.makeText(getApplicationContext(), "文件下载完毕", 0).show();
                break;
            case SHOW_TEXT:
                Toast.makeText(getApplicationContext(),
                        "下载进度" + pb.getProgress() * 100 / pb.getMax() + "%", 0)
                        .show();
                break;
            default:
                break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_path = (EditText) findViewById(R.id.et_path);
        pb = (ProgressBar) findViewById(R.id.pd);
        tv = (TextView) findViewById(R.id.tv);
    }

    public void downLoad(View view) {
        String path = et_path.getText().toString().trim();
        final int thread = 3;
        if (TextUtils.isEmpty(path)) {
            Toast.makeText(this, "路径读取失败", 0).show();
            return;
        }
        new Thread() {
            public void run() {
                try {
                    // 1:连接服务器,获取一个文件,获取文件的长度,在本地创建一个大小跟服务器文件大小
                    String path = "http://dldx.csdn.net/fd.php?i=545080895071440&s=28f87f3d754b743fc81fb8b82848dcd1";
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url
                            .openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setRequestMethod("GET");
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        // 服务器返回的数据的长度
                        int length = conn.getContentLength();
                        pb.setMax(length);// 设置进度条的最大值
                        System.out.println("文件总长度" + length);
                        // 在客户端本地创建出来一个大小跟服务器端文件大小的临时文件
                        RandomAccessFile raFile = new RandomAccessFile(
                                "/sdcard/spider_demo.rar", "rwd");
                        // 指定创建的这个文件的长度
                        raFile.setLength(length);
                        raFile.close();
                        // 平均每一个线程下载的文件的大小
                        int blockSize = length / thread;
                        for (int i = 1; i <= thread - 1; i++) {
                            // 第一个线程下载的开始位置
                            int startIndex = (i - 1) * blockSize;
                            int endIndex = i * blockSize - 1;
                            if (i == thread) {// 最后一个线程的长度要稍微长一点
                                endIndex = length;
                            }
                            System.out.println("线程:" + i + "下载:----"
                                    + startIndex + "----->" + endIndex);
                            new DownLoadthread(i, startIndex, endIndex, path)
                                    .start();
                        }
                    } else {
                        Message msg = new Message();
                        msg.what = SERVER_ERROR;
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = DOWN_LOAD_ERROR;
                    handler.sendMessage(msg);
                }
            };

        }.start();
    }

    /**
     * 下载文件的子线程,每一个线程下载对应位置的文件
     * 
     * */
    public class DownLoadthread extends Thread {
        private int threadId;
        private int startIndex;
        private int endIndex;
        private String path;

        /**
         * @param threadId线程id
         * @param startIndex
         *            线程下载的开始位置
         * @param endIndex
         *            线程下载的结束位置
         * @param path下载文件在服务器上的路径
         * 
         * */
        public DownLoadthread(int threadId, int startIndex, int endIndex,
                String path) {
            this.threadId = threadId;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.path = path;
        }

        @Override
        public void run() {
            try {
                // 检查是否存在记录下载的文件,如果存在读取这个文件的数据
                // ----------------------替换成数据库----------------------------
                File tempFile = new File("/sdcard/" + threadId + ".txt");
                if (tempFile.exists() && tempFile.length() > 0) {
                    FileInputStream fis = new FileInputStream(tempFile);
                    byte[] temp = new byte[1024];
                    int leng = fis.read(temp);
                    String downloadLen = new String(temp, 0, leng);
                    int downloadInt = Integer.parseInt(downloadLen);
                    startIndex += downloadInt;// 修改下载的真正的开始位置
                    fis.close();
                }
                // ----------------------替换成数据库----------------------------
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("GET");
                // 重要:请求服务器下载部分的文件,需要置顶文件的位置
                conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
                        + endIndex);
                // 从服务器请求全部资源,如果从服务器请求部分资源为206
                int code = conn.getResponseCode();
                System.out.println("code=" + code);
                /*
                 * if (code == 200) { InputStream is =
                 * conn.getInputStream();//返回全部的资源 }
                 */
                InputStream is = conn.getInputStream();// 返回部分的资源
                RandomAccessFile raFile = new RandomAccessFile(
                        "/sdcard/spider_demo.rar", "rwd");
                // 随机写文件的时候,从哪个文件开始写
                raFile.seek(startIndex);
                int len = 0;
                byte[] buffer = new byte[1024];
                int total = 0;// 已经下载的数据的长度
                File file = new File("/sdcard/" + threadId + ".txt");// 作用:记录当前线程下载的数据长度
                while ((len = is.read(buffer)) != -1) {
                    // 如果你是固态硬盘,那么必须这样写数据
                    // RandomAccessFile info = new RandomAccessFile(
                    // threadId + ".txt", "rwd");
                    FileOutputStream fos = new FileOutputStream(file);
                    raFile.write(buffer, 0, len);
                    total += len;
                    fos.write(String.valueOf(total).getBytes());
                    fos.close();
                    // 更新进度条
                    synchronized (MainActivity.this) {
                        currentProcess += len;// 获取所以进程下载的总进度总进度
                        pb.setProgress(currentProcess);// 更该界面上progressbar进度条的进度
                        Message message = Message.obtain();// 返回一个消息实例,防止创建太多对象
                        message.what = SHOW_TEXT;
                        // message.obj
                        handler.sendMessage(message);
                        // 特殊情况,progressbar
                        // progressdialog进度条对话框可以直接在子线程里面更新界面,它内部处理过了
                    }
                }
                is.close();
                raFile.close();
                System.out.println("线程:" + threadId + "下载完毕了...");
                File deleteFile = new File("/sdcard/" + threadId + ".txt");
                deleteFile.delete();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                threadFinish();
            }
        }

        private synchronized void threadFinish() {
            runningThread--;
            if (runningThread == 0) {
                for (int i = 1; i <= 3; i++) {
                    File file = new File("/sdcard/" + i + ".txt");
                    file.delete();
                }
                System.out.println("文件下载完毕,删除所以的下载记录");
                Message msg = new Message();
                msg.what = DOWN_LOAD_FINSIH;
                handler.sendMessage(msg);
            }
        }
    }
}

在清单文件里面加入权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.thread"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最终显示断点下载

 

posted on 2014-11-11 14:59  java界的奥特曼  阅读(249)  评论(0编辑  收藏  举报