多线程下载文件

Posted 西西弗斯丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程下载文件相关的知识,希望对你有一定的参考价值。

多线程下载文件

DBOpenHelper.java

package com.amlapp.update.otaupgrade.download;

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

public class DBOpenHelper extends SQLiteOpenHelper {
    // 数据库文件的文件名 
    private static final String DBNAME = "download.db";
    // 数据库的版本号
    private static final int VERSION = 1;

    public DBOpenHelper(Context context) {
        super(context, DBNAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS filedownlog (id integer primary key autoincrement, downpath varchar(100), threadid INTEGER, downlength INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS filedownlog");
        onCreate(db);
    }
}

DownloadThread.java

package com.amlapp.update.otaupgrade.download;

import java.io.BufferedInputStream; 
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import android.util.Log;

public class DownloadThread extends Thread {
    private static final String TAG = "DownloadThread";
    /** 本地保存文件 */
    private File saveFile;
    /** 下载路径 */
    private URL downUrl;
    /** 该线程要下载的长度 */
    private int block;
    /** 线程ID */
    private int threadId = -1;
    /** 该线程已经下载的长度 */
    private int downLength;
    /** 是否下载完成 */
    private boolean finish = false;
    /** 文件下载器 */
    private FileDownloader downloader;

    /***
     * 构造方法
     */
    public DownloadThread(FileDownloader downloader, URL downUrl,
            File saveFile, int block, int downLength, int threadId) {
        this.downUrl = downUrl;
        this.saveFile = saveFile;
        this.block = block;
        this.downloader = downloader;
        this.threadId = threadId;
        this.downLength = downLength;
    }

    /**
     * 线程主方法
     */
    @Override
    public void run() {
        if (downLength < block) {// 未下载完成
            try {
                HttpURLConnection http = (HttpURLConnection) downUrl
                        .openConnection();
                http.setConnectTimeout(5 * 1000);
                http.setRequestMethod("GET");
                http.setRequestProperty(
                        "Accept",
                        "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash,"
                                + " application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                                + "application/x-ms-application, application/vnd.ms-excel,"
                                + " application/vnd.ms-powerpoint, application/msword, */*");
                http.setRequestProperty("Accept-Language", "zh-CN");
                http.setRequestProperty("Referer", downUrl.toString());
                http.setRequestProperty("Charset", "UTF-8");
                // 该线程开始下载位置
                int startPos = block * (threadId - 1) + downLength;
                // 该线程下载结束位置
                int endPos = block * threadId - 1;
                // 设置获取实体数据的范围
                http.setRequestProperty("Range", "bytes=" + startPos + "-"
                        + endPos);
                http.setRequestProperty(
                        "User-Agent",
                        "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0;"
                                + " .NET CLR 1.1.4322; .NET CLR 2.0.50727; "
                                + ".NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
                http.setRequestProperty("Connection", "Keep-Alive");
                /****/
                System.out.println("DownloadThread http.getResponseCode():"
                        + http.getResponseCode());
                if (http.getResponseCode() == 206) {
                    /***
                     * //获取输入流 InputStream inStream = http.getInputStream();
                     * byte[] buffer = new byte[1024]; int offset = 0;
                     * print("Thread " + this.threadId +
                     * " start download from position " + startPos);
                     * 
                     * // rwd: 打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到基础存储设备。
                     * //对于Android移动设备一定要注意同步,否则当移动设备断电的话会丢失数据 RandomAccessFile
                     * threadfile = new RandomAccessFile( this.saveFile, "rwd");
                     * //直接移动到文件开始位置下载的 threadfile.seek(startPos); while
                     * (!downloader.getExit() && (offset = inStream.read(buffer,
                     * 0, 1024)) != -1) { threadfile.write(buffer, 0,
                     * offset);//开始写入数据到文件 downLength += offset; //该线程以及下载的长度增加
                     * downloader.update(this.threadId,
                     * downLength);//修改数据库中该线程已经下载的数据长度
                     * downloader.append(offset);//文件下载器已经下载的总长度增加 }
                     * threadfile.close();
                     * 
                     * print("Thread " + this.threadId + " download finish");
                     * this.finish = true;
                     **/
                    // 获取输入流
                    InputStream inStream = http.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(inStream);
                    byte[] buffer = new byte[1024 * 4];
                    int offset = 0;
                    RandomAccessFile threadfile = new RandomAccessFile(
                            this.saveFile, "rwd");
                    // 获取RandomAccessFile的FileChannel
                    FileChannel outFileChannel = threadfile.getChannel();
                    // 直接移动到文件开始位置下载的
                    outFileChannel.position(startPos);
                    // 分配缓冲区的大小
                    while (!downloader.getExit()
                            && (offset = bis.read(buffer)) != -1) {
                        outFileChannel
                                .write(ByteBuffer.wrap(buffer, 0, offset));// 开始写入数据到文件
                        downLength += offset; // 该线程以及下载的长度增加
                        downloader.update(this.threadId, downLength);// 修改数据库中该线程已经下载的数据长度
                        downloader.append(offset);// 文件下载器已经下载的总长度增加
                    }
                    outFileChannel.close();
                    threadfile.close();
                    inStream.close();
                    print("Thread " + this.threadId + " download finish");
                    this.finish = true;
                }
            } catch (Exception e) {
                this.downLength = -1;
                print("Thread " + this.threadId + ":" + e);
            }
        }
    }

    private static void print(String msg) {
        Log.i(TAG, msg);
    }

    /**
     * 下载是否完成
     * 
     * @return
     */
    public boolean isFinish() {
        return finish;
    }

    /**
     * 已经下载的内容大小
     * 
     * @return 如果返回值为-1,代表下载失败
     */
    public long getDownLength() {
        return downLength;
    }
}

FileDownloader.java

package com.amlapp.update.otaupgrade.download;

import java.io.BufferedReader;
import java.io.File; 
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams; 

import android.content.Context;
import android.util.Log;

/**
 * 文件下载器
 */
public class FileDownloader {
    private static final String TAG = "FileDownloader";
    /** 上下文 */
    private Context context;
    /** 文件下载服务类 */
    private FileService fileService;
    /** 是否停止下载 */
    private boolean exit;
    /** 已下载文件长度 */
    private int downloadSize = 0;
    /** 原始文件长度 */
    private int fileSize = 0;
    /** 用于下载的线程数组 */
    private DownloadThread[] threads;
    /** 本地保存文件 */
    private File saveFile;
    /** 缓存各线程下载的长度 */
    private Map<Integer, Integer> data = new ConcurrentHashMap<Integer, Integer>();
    /** 每条线程下载的长度 */
    private int block;
    /** 下载路径 */
    private String downloadUrl;


    /**
     * 构建文件下载器
     * 
     * @param downloadUrl
     *            下载路径
     * @param fileSaveDir
     *            文件保存目录
     * @param threadNum
     *            下载线程数
     */
    public FileDownloader(Context context, String downloadUrl,
            File fileSaveDir, int threadNum) {
        try {
            this.context = context;
            this.downloadUrl = downloadUrl;
            fileService = new FileService(this.context);
            // 根据指定的下载路径,生成URL
            URL url = new URL(this.downloadUrl);
            if (!fileSaveDir.exists())
                fileSaveDir.mkdirs();// 如果保存路径不存在,则新建一个目录
            // 根据指定的线程数来新建线程数组
            this.threads = new DownloadThread[threadNum];
            // 打开HttpURLConnection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置 HttpURLConnection的断开时间
            conn.setConnectTimeout(5 * 1000);
            // 设置 HttpURLConnection的请求方式
            conn.setRequestMethod("GET");
            // 设置 HttpURLConnection的接收的文件类型
            conn.setRequestProperty(
                    "Accept",
                    "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
                            + "application/x-shockwave-flash, application/xaml+xml, "
                            + "application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, "
                            + "application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            // 设置 HttpURLConnection的接收语音
            conn.setRequestProperty("Accept-Language", "zh-CN");
            // 指定请求uri的源资源地址
            conn.setRequestProperty("Referer", downloadUrl);
            // 设置 HttpURLConnection的字符编码
            conn.setRequestProperty("Charset", "UTF-8");
            // 检查浏览页面的访问者在用什么操作系统(包括版本号)浏览器(包括版本号)和用户个人偏好
            conn.setRequestProperty(
                    "User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2;"
                            + " Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; "
                            + ".NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152;"
                            + " .NET CLR 3.5.30729)");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.connect();
            // 打印Http协议头
            printResponseHeader(conn);
            // 如果返回的状态码为200表示正常
            //System.out.println("conn.getResponseCode():"+conn.getResponseCode());
            if (conn.getResponseCode() == 200) {
                this.fileSize = conn.getContentLength();// 根据响应获取文件大小
                if (this.fileSize <= 0)
                    throw new RuntimeException("Unkown file size ");

                String filename = getFileName(conn);// 获取文件名称
                this.saveFile = new File(fileSaveDir, filename);// 构建保存文件
                Map<Integer, Integer> logdata = fileService
                        .getData(downloadUrl);// 获取下载记录
                if (logdata.size() > 0) {// 如果存在下载记录
                    for (Map.Entry<Integer, Integer> entry : logdata.entrySet())
                        data.put(entry.getKey(), entry.getValue());// 把各条线程已经下载的数据长度放入data中
                }
                if (this.data.size() == this.threads.length) {// 下面计算所有线程已经下载的数据总长度
                    for (int i = 0; i < this.threads.length; i++) {
                        this.downloadSize += this.data.get(i + 1);
                    }
                    print("已经下载的长度" + this.downloadSize);
                }
                // 计算每条线程下载的数据长度
                this.block = (this.fileSize % this.threads.length) == 0 ? this.fileSize
                        / this.threads.length
                        : this.fileSize / this.threads.length + 1;
            } else {
                throw new RuntimeException("server no response ");
            }
        } catch (Exception e) {
            print(e.toString());
            throw new RuntimeException("don‘t connection this url");
        }
    }

    /**
     * 获取线程数
     */
    public int getThreadSize() {
        return threads.length;
    }

    /**
     * 退出下载
     */
    public void exit() {
        this.exit = true;
    }

    /**
     * 是否退出下载
     */
    public boolean getExit() {
        return this.exit;
    }

    /**
     * 获取文件大小
     */
    public int getFileSize() {
        return fileSize;
    }

    /**
     * 累计已下载大小
     * 该方法在具体某个线程下载的时候会被调用
     */
    protected synchronized void append(int size) {
        downloadSize += size;
    }

    /**
     * 更新指定线程最后下载的位置
     * 该方法在具体某个线程下载的时候会被调用
     * @param threadId
     *            线程id
     * @param pos
     *            最后下载的位置
     */
    protected synchronized void update(int threadId, int pos) {
        // 缓存各线程下载的长度
        this.data.put(threadId, pos);
        // 更新数据库中的各线程下载的长度
        this.fileService.update(this.downloadUrl, threadId, pos);
    }



    /**
     * 获取文件名
     * 
     * @param conn
     *            Http连接
     */
    private String getFileName(HttpURLConnection conn) {
        String filename = this.downloadUrl.substring(this.downloadUrl
                .lastIndexOf(‘/‘) + 1);// 截取下载路径中的文件名
        // 如果获取不到文件名称
        if (filename == null || "".equals(filename.trim())) {
            // 通过截取Http协议头分析下载的文件名
            for (int i = 0;; i++) {
                String mine = conn.getHeaderField(i);
                if (mine == null)
                    break;
                /**
                 * Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME
                 * 用户代理如何显示附加的文件。
                 * Content-Disposition就是当用户想把请求所得的内容存为一个文件的时候提供一个默认的文件名
                 * 协议头中的Content-Disposition格式如下:
                 * Content-Disposition","attachment;filename=FileName.txt");
                 */
                if ("content-disposition".equals(conn.getHeaderFieldKey(i)
                        .toLowerCase())) {
                    // 通过正则表达式匹配出文件名
                    Matcher m = Pattern.compile(".*filename=(.*)").matcher(
                            mine.toLowerCase());
                    // 如果匹配到了文件名
                    if (m.find())
                        return m.group(1);// 返回匹配到的文件名
                }
            }
            // 如果还是匹配不到文件名,则默认取一个随机数文件名
            filename = UUID.randomUUID() + ".tmp";
        }
        return filename;
    }

    /**
     * 开始下载文件
     * 
     * @param listener
     *            监听下载数量的变化,如果不需要了解实时下载的数量,可以设置为null
     * @return 已下载文件大小
     * @throws Exception
     */
    public int download(DownloadProgressListener listener) throws Exception {
        try {
            RandomAccessFile randOut = new RandomAccessFile(this.saveFile, "rw");
            if (this.fileSize > 0)
                randOut.setLength(this.fileSize);
            randOut.close();
            URL url = new URL(this.downloadUrl);
            // 如果原先未曾下载或者原先的下载线程数与现在的线程数不一致
            if (this.data.size() != this.threads.length) {
                this.data.clear();// 清除原来的线程数组
                for (int i = 0; i < this.threads.length; i++) {
                    this.data.put(i + 1, 0);// 初始化每条线程已经下载的数据长度为0
                }
                this.downloadSize = 0;
            }
            //循环遍历线程数组
            for (int i = 0; i < this.threads.length; i++) {
                int downLength = this.data.get(i + 1); // 获取当前线程下载的文件长度
                // 判断线程是否已经完成下载,否则继续下载
                if (downLength < this.block
                        && this.downloadSize < this.fileSize) {
                    //启动线程开始下载
                    this.threads[i] = new DownloadThread(this, url,
                            this.saveFile, this.block, this.data.get(i + 1),
                            i + 1);
                    this.threads[i].setPriority(7);
                    this.threads[i].start();
                } else {
                    this.threads[i] = null;
                }
            }
            //如果存在下载记录,从数据库中删除它们
            fileService.delete(this.downloadUrl);
            //重新保存下载的进度到数据库
            fileService.save(this.downloadUrl, this.data);
            boolean notFinish = true;// 下载未完成
            while (notFinish) {// 循环判断所有线程是否完成下载
                Thread.sleep(900);
                notFinish = false;// 假定全部线程下载完成
                for (int i = 0; i < this.threads.length; i++) {
                    if (this.threads[i] != null && !this.threads[i].isFinish()) {// 如果发现线程未完成下载
                        notFinish = true;// 设置标志为下载没有完成
                        // 如果下载失败,再重新下载
                        if (this.threads[i].getDownLength() == -1) {
                            this.threads[i] = new DownloadThread(this, url,
                                    this.saveFile, this.block,
                                    this.data.get(i + 1), i + 1);
                            this.threads[i].setPriority(7);
                            this.threads[i].start();
                        }
                    }
                }
                if (listener != null)
                    listener.onDownloadSize(this.downloadSize,this.fileSize);// 通知目前已经下载完成的数据长度
            }
            // 如果下载完成
            if (downloadSize == this.fileSize)
                fileService.delete(this.downloadUrl);// 下载完成删除记录
        } catch (Exception e) {
            print(e.toString());
            throw new Exception("file download error");
        }
        return this.downloadSize;
    }

    /**
     * 获取Http响应头字段
     * @param http
     * @return
     */
    public static Map<String, String> getHttpResponseHeader(
            HttpURLConnection http) {
        Map<String, String> header = new LinkedHashMap<String, String>();
        for (int i = 0;; i++) {
            String mine = http.getHeaderField(i);
            if (mine == null)
                break;
            header.put(http.getHeaderFieldKey(i), mine);
        }
        return header;
    }

    /**
     * 打印Http头字段
     * 
     * @param http
     */
    public static void printResponseHeader(HttpURLConnection http) {
        Map<String, String> header = getHttpResponseHeader(http);
        for (Map.Entry<String, String> entry : header.entrySet()) {
            String key = entry.getKey() != null ? entry.getKey() + ":" : "";
            print(key + entry.getValue());
        }
    }


    /**
    * 获取网址内容
    * @param url
    * @return
    * @throws Exception
    */
    public static String getContent(String url) throws Exception{
        StringBuilder sb = new StringBuilder();

        HttpClient client = new DefaultHttpClient();
        HttpParams httpParams = client.getParams();
        //设置网络超时参数
        HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
        HttpConnectionParams.setSoTimeout(httpParams, 5000);
        HttpResponse response = client.execute(new HttpGet(url));
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8192);

            String line = null;
            while ((line = reader.readLine())!= null){
                sb.append(line + "\n");
            }
            reader.close();
        }
        return sb.toString();
    } 



    /**
     * 打印信息
     * @param msg  信息
     */
    private static void print(String msg) {
        Log.i(TAG, msg);
    }

    /** 
     * 下载进度监听接口
     */
    public interface DownloadProgressListener {
        /**
         *下载的进度 
         */
        public void onDownloadSize(int size,int fileSize);
    }

}

FileService.java

package com.amlapp.update.otaupgrade.download;

import java.util.HashMap;
import java.util.Map;

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

/**
 * 文件下载服务类
 */
public class FileService { 
    private DBOpenHelper openHelper;

    public FileService(Context context) {
        openHelper = new DBOpenHelper(context);
    }

    /**
     * 获取每条线程已经下载的文件长度
     * 
     * @param path
     * @return
     */
    public Map<Integer, Integer> getData(String path) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db
                .rawQuery(
                        "select threadid, downlength from filedownlog where downpath=?",
                        new String[] { path });
        Map<Integer, Integer> data = new HashMap<Integer, Integer>();
        while (cursor.moveToNext()) {
            data.put(cursor.getInt(0), cursor.getInt(1));
        }
        cursor.close();
        db.close();
        return data;
    }

    /**
     * 保存每条线程已经下载的文件长度
     * 
     * @param path
     * @param map
     */
    public void save(String path, Map<Integer, Integer> map) {// int threadid,
                                                                // int position
        SQLiteDatabase db = openHelper.getWritableDatabase();
        db.beginTransaction();
        try {
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                db.execSQL(
                        "insert into filedownlog(downpath, threadid, downlength) values(?,?,?)",
                        new Object[] { path, entry.getKey(), entry.getValue() });
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
        db.close();
    }

    /**
     * 实时更新每条线程已经下载的文件长度
     * 
     * @param path
     * @param map
     */
    public void update(String path, int threadId, int pos) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        db.execSQL(
                "update filedownlog set downlength=? where downpath=? and threadid=?",
                new Object[] { pos, path, threadId });
        db.close();
    }

    /**
     * 当文件下载完成后,删除对应的下载记录
     * 
     * @param path
     */
    public void delete(String path) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        db.execSQL("delete from filedownlog where downpath=?",
                new Object[] { path });
        db.close();
    }
}

测试Activity:

package com.moonlight.projectorforge;

import java.io.File;

import com.amlapp.update.otaupgrade.download.FileDownloader;
import com.amlapp.update.otaupgrade.download.FileDownloader.DownloadProgressListener;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new AsyncTask<String, Integer, Integer>() {
            private String downloadUrl = "http://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=asda&step_word=&pn=1&spn=0&di=145126455210&pi=&rn=1&tn=baiduimagedetail&is=&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=3541143192%2C3632970612&os=3611388031%2C2854364844&simid=3432062895%2C349837288&adpicid=0&ln=1976&fr=&fmq=1467017028245_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fwww.hereinuk.com%2Fwp-content%2Fuploads%2F2014%2F07%2Fasda.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3F4r_z%26e3Bojtxtg_z%26e3Bqq_z%26e3Bv54AzdH3Ff%3F__ktz%3DMzA9OTQaMTUzOA%3D%3D%264t1%3Ddaan8b99m%26t1x%3Dd%26fg%3Dkwknnw0ud19bv989jbm0c8m8dcknkdn0&gsm=0&rpstart=0&rpnum=0";
            private File fileSaveDir = new File("/storage/external_storage/sda1/");
            private int threadNum = 5;
            private int totalSize;

            @Override
            protected Integer doInBackground(String... params) {
                FileDownloader loader = new FileDownloader(MainActivity.this, downloadUrl, fileSaveDir, threadNum);
                try {
                    loader.download(new DownloadProgressListener() {
                        @Override
                        public void onDownloadSize(int size, int fileSize) {
                            totalSize = fileSize;
                            publishProgress(size);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    return -1;
                }
                return 0;
            }
            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                //Todo show download progress,percentage:100f*values[0]/totalSize
            }
            @Override
            protected void onPostExecute(Integer result) {
                super.onPostExecute(result);
                if(result ==0){
                    //Todo download success
                }else{
                    //Todo download failed
                }

            }
        }.execute();
    }

}

以上是关于多线程下载文件的主要内容,如果未能解决你的问题,请参考以下文章

自己开发的在线视频下载工具,基于Java多线程

多线程实现多线程下载文件

多线程下载

多线程 Thread 线程同步 synchronized

Java多线程断点下载文件并压缩

安卓 多线程下载文件