視頻圖片--多線程下載工具

還嫌網速慢? 那是由於你沒有一個好的下載工具, 多線程下載, 線程個數本身定義, 想多塊就多快,一塊兒來看看吧!!!java

多線程使用線程計數同步輔助,同步計算多線程個數,若是線程下載超時, 支持從新下載,方便使用.服務器

 

 

 

 

1.多線程工具類:   MutiThreadDownLoad.java

 

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

public class MutiThreadDownLoad {
    // 同時下載的線程數
    private int threadCount;
    // 服務器請求路徑
    private String serverPath;
    //記錄下載完成的次數
    public static int number_thread=50;
    //本地路徑
    private String localPath;
    //線程計數同步輔助
    private CountDownLatch latch;
 
    public MutiThreadDownLoad(int threadCount, String serverPath, String localPath, CountDownLatch latch) {
        this.number_thread=threadCount;
        this.threadCount = threadCount;
        this.serverPath = serverPath;
        this.localPath = localPath;
        this.latch = latch;
    }

 
    public boolean executeDownLoad() {
        try {
            URL url = new URL(serverPath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            int code = conn.getResponseCode();
            if (code == 200) {
                //服務器返回的數據的長度,實際上就是文件的長度,單位是字節
                int length = conn.getContentLength();
                System.out.println("文件總長度:" + length + "字節(B)");
                if (length == 0) {
                    return false;
                }

                RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
                //指定建立的文件的長度
                raf.setLength(length);
                raf.close();
                //分割文件
                int blockSize = length / threadCount;
                for (int threadId = 1; threadId <= threadCount; threadId++) {
                    //第一個線程下載的開始位置
                    int startIndex = (threadId - 1) * blockSize;
                    int endIndex = startIndex + blockSize - 1;
                    if (threadId == threadCount) {
                        //最後一個線程下載的長度稍微長一點
                        endIndex = length;
                    }

                    System.out.println("線程" + threadId + "下載:" + startIndex + "字節~" + endIndex + "字節");
                    new DownLoadThread(threadId, startIndex, endIndex).start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
      return true;
    }


    /**

     * 內部類用於實現下載

     */

    public class DownLoadThread extends Thread {
        //線程ID
        private int threadId;
        //下載起始位置
        private int startIndex;
        //下載結束位置
        private int endIndex;

        public DownLoadThread(int threadId, int startIndex, int endIndex) {
            this.threadId = threadId;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }

        @Override
        public void run() {
            down_time();
        }

 

        /**

         * 若是超時,或者下載失敗,就使用遞歸從新下載

         */

        public void down_time(){
            try {
                System.out.println("線程" + threadId + "正在下載...");
                URL url = new URL(serverPath);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                //請求服務器下載部分的文件的指定位置
                conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
                conn.setConnectTimeout(10000);
                conn.setReadTimeout(10000);
                int code = conn.getResponseCode();
                System.out.println("線程" + threadId + "請求返回code=" + code);
                InputStream is = conn.getInputStream();//返回資源
                RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
                //隨機寫文件的時候從哪一個位置開始寫
                raf.seek(startIndex);//定位文件
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    raf.write(buffer, 0, len);
                }
                is.close();
                raf.close();
                number_thread=number_thread-1;
                System.out.println("線程" + threadId + "下載完畢  "+number_thread);
                //計數值減一
                latch.countDown();
            } catch (Exception e) {
                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa").format(new Date())+":線程 "+threadId + "訪問超時,從新下載!!");
                down_time();
            }
        }
    }
}

 

 

 

2.封裝工具類方法:

   public static boolean download(String remoteUrl,String localUrl){
    boolean bd=true;
    int threadSize = 50;
    String serverPath = remoteUrl;
    String localPath = localUrl;
    long startTime = System.currentTimeMillis();
    CountDownLatch latch = new CountDownLatch(threadSize);
    MutiThreadDownLoad m = new MutiThreadDownLoad(threadSize, serverPath, localPath, latch);
    try {
        boolean x = m.executeDownLoad();
        //若是文件的長度等於0,則直接跳過等待,不提示錯誤
        if (x){
            latch.await();
        }else{
            bd=false;//下載失敗
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    long endTime = System.currentTimeMillis();
    System.out.println("所有下載結束,共耗時" + (endTime - startTime) / 1000 + "s");
    return bd;
}

 

 

3.使用示例:

String urlss="";//要下載的網絡資源路徑網絡

String urltt="";//要存放的本地路徑多線程

download(urlss,urltt);dom

相關文章
相關標籤/搜索