Video picture -- multi thread download tool

Don't think the network speed is slow? That's because you don't have a good download tool, multi-threaded download, the number of threads defined by yourself, how fast you want to multi block, let's have a look!!!

Multithreading uses thread count synchronization assistant to synchronously calculate the number of multithreads. If the thread download times out, it supports re download and is convenient to use

 

 

 

 

1. Multithread tool class: 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 {
    // Number of threads downloaded at the same time
    private int threadCount;
    // Server request path
    private String serverPath;
    //Record the number of downloads completed
    public static int number_thread=50;
    //Local path
    private String localPath;
    //Thread count synchronization auxiliary
    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) {
                //The length of the data returned by the server is actually the length of the file,Unit is byte
                int length = conn.getContentLength();
                System.out.println("Total file length:" + length + "byte(B)");
                if (length == 0) {
                    return false;
                }

                RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
                //Specifies the length of the file created
                raf.setLength(length);
                raf.close();
                //Split file
                int blockSize = length / threadCount;
                for (int threadId = 1; threadId <= threadCount; threadId++) {
                    //Start of first thread Download
                    int startIndex = (threadId - 1) * blockSize;
                    int endIndex = startIndex + blockSize - 1;
                    if (threadId == threadCount) {
                        //The length of the last thread download is a little longer
                        endIndex = length;
                    }

                    System.out.println("thread" + threadId + "download:" + startIndex + "byte~" + endIndex + "byte");
                    new DownLoadThread(threadId, startIndex, endIndex).start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
      return true;
    }


    /**

     * Inner class for download

     */

    public class DownLoadThread extends Thread {
        //thread ID
        private int threadId;
        //Download from
        private int startIndex;
        //Download end location
        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();
        }

 

        /**

         * If it times out, or the download fails, use recursion to download again

         */

        public void down_time(){
            try {
                System.out.println("thread" + threadId + "Downloading...");
                URL url = new URL(serverPath);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                //The specified location of the file requesting the server to download part
                conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
                conn.setConnectTimeout(10000);
                conn.setReadTimeout(10000);
                int code = conn.getResponseCode();
                System.out.println("thread" + threadId + "Request return code=" + code);
                InputStream is = conn.getInputStream();//Return resources
                RandomAccessFile raf = new RandomAccessFile(localPath, "rwd");
                //Where to start when writing files at random
                raf.seek(startIndex);//Location file
                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("thread" + threadId + "Download completed  "+number_thread);
                //Count minus one
                latch.countDown();
            } catch (Exception e) {
                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss aa").format(new Date())+":thread "+threadId + "Access timeout,Re Download!!");
                down_time();
            }
        }
    }
}

 

 

 

2. Encapsulation tool method:

   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();
        //If the length of the file is equal to 0,Skip waiting directly,Don't prompt for errors
        if (x){
            latch.await();
        }else{
            bd=false;//Download failed
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    long endTime = System.currentTimeMillis();
    System.out.println("End of download all,Total time consuming" + (endTime - startTime) / 1000 + "s");
    return bd;
}

 

 

3. Use example:

String urlss =; / / network resource path to download

String urltt =; / / local path to store

download(urlss,urltt);

Tags: Java network

Posted on Wed, 04 Dec 2019 04:57:39 -0500 by Jem