多線程下載 斷點續傳

package wml.dl;java

import java.io.BufferedInputStream;dom

import java.io.FileNotFoundException;ide

import java.io.IOException;ui

import java.io.InputStream;this

import java.net.HttpURLConnection;spa

import java.util.Properties;.net

 import wml.dl.io.BufferedRandomOutputStream;線程

 * 下載線程,負責下載DLBlockorm

 * @author wml圖片

public class DLThread implements Runnable {

    private static final int INPUT_BUFFER_SIZE = 1024;

    private static final int OUTPUT_BUFFER_SIZE = 8192;  

    private DLTask task;

    private DLBlock block;

    private boolean isDone;

    private boolean isStop;

    public DLBlock getBlock() {

        return block;

    public DLThread(DLTask task, DLBlock block) {

        this.task = task;

        this.block = block;

    @Override

    public void run() { 

        block.setStatus(DLBlock.STATUS_RUNNING);

        BufferedRandomOutputStream randomOutputStream = null;

        try {  

            randomOutputStream = new BufferedRandomOutputStream(task.openRandomAccessFile(), OUTPUT_BUFFER_SIZE);   

        } catch (FileNotFoundException e) {

            block.setThrowable(e);

            e.printStackTrace();

            isDone = true;

            return;

        HttpURLConnection conn = null;

        long downloadPosition = 0;

        try { 

            if(task.isSupportedContentRange()) {//斷點續傳

                block.setDownloaded(Math.max(block.getDownloaded() - OUTPUT_BUFFER_SIZE, 0));// -8kb保證下載結果(有可能以前寫入失敗)           

                downloadPosition = block.getBegin() + block.getDownloaded();     

                randomOutputStream.seek(downloadPosition);

            Properties reqProps = new Properties();

            if(task.isSupportedContentRange()) {//設置下載範圍

                reqProps.setProperty("Range", "bytes=" + downloadPosition + "-" + block.getEnd());http://www.huiyi8.com/gundongdaima/圖片滾動代碼 

            conn = DLUtils.getChromeURLConnection(task.getUri(), false, reqProps);

            InputStream in = new BufferedInputStream(DLUtils.getInputStream(conn.getInputStream(), conn.getContentEncoding()), INPUT_BUFFER_SIZE);

            byte[] buf = new byte[INPUT_BUFFER_SIZE];

            int len;     

            while((len = in.read(buf)) != -1) {

     randomOutputStream.write(buf, 0, len);

                block.updateDownloaded(len);

                 

                downloadPosition += len;

                 

                if(task.isSupportedContentRange() && downloadPosition >= block.getEnd()

                        || isStop

                        || !task.isRunning()) {

                    //也許將任務部分分配給了其餘線程(增長了線程),在後面作詳細處理

                    break;

        } catch (IOException e) {

             

            block.setThrowable(e);

            e.printStackTrace();

             

        } finally {

 

            DLUtils.close(conn);

            DLUtils.close(randomOutputStream);

             

            isDone = true;

             

            if(downloadPosition >= block.getEnd()) {

     

                //塊下載完成

                block.setStatus(DLBlock.STATUS_DONE);

                 

            } else {

                 

                //中止下載

                block.setStatus(DLBlock.STATUS_PAUSE);

            try {//檢查並設置任務狀態

                task.checkDone();

            } catch (FileNotFoundException e) {

                 

                block.setThrowable(e);

                e.printStackTrace();

    public boolean isDone() {

        return isDone;

    }

 

    public void stop() {

        isStop = true;

    }

相關文章
相關標籤/搜索