多線程使用線程池分割下載同一個文件,通過斷點續傳合併成一個文件

public class Main {

    private static final String url = "https://d9f43c992961ae0750505398dbb4823c.dd.cdntips.com/imtt.dd.qq.com/16891/apk/9C52689F0451CBFBDBBF535ADD40D3A7.apk?mkey=5e9fdcc9777b5f80&f=24c3&fsname=com.tencent.weishi_6.7.6.588_676.apk&csr=1bbd&cip=119.123.121.117&proto=https";

    public static void main(String[] args) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("connection", "keep-alive");
        int fileLength = conn.getContentLength();
        System.out.println("文件長度:" + fileLength);
        int theadCount = 3;
        int size = fileLength / theadCount;
        System.out.println("每個下載量:" + size);
        conn.disconnect();
        File file = new File("D:\\ws.apk");
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        MyTask task0 = new MyTask(0, 0, size - 1, file);
        MyTask task1 = new MyTask(1, size, (2 * size) - 1, file);
        MyTask task2 = new MyTask(2, 2 * size, fileLength, file);
        fixedThreadPool.execute(task0);
        fixedThreadPool.execute(task1);
        fixedThreadPool.execute(task2);
        fixedThreadPool.shutdown();
    }

    static class MyTask implements Runnable {

        private final int taskNum;
        private final int startPos;
        private final int endPos;
        private final File file;

        public MyTask(int taskNum, int startPos, int endPos, File file) {
            this.taskNum = taskNum;
            this.startPos = startPos;
            this.endPos = endPos;
            this.file = file;
        }

        @Override
        public void run() {
            try {
                HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty("connection", "keep-alive");
                //設置下載區間
                conn.setRequestProperty("range", "bytes=" + startPos + "-" + endPos);

                conn.connect();
                int code = conn.getResponseCode();
                if (code == 206) {
                    InputStream in = conn.getInputStream();
                    int serverSize = conn.getContentLength();
                    System.out.println("服務器返回的長度:" + serverSize);
                    System.out.println("bytes=" + startPos + "-" + endPos);
                    System.out.println(Thread.currentThread().getName() + "---" + taskNum);
               
                    RandomAccessFile out = new RandomAccessFile(file, "rw");
                    out.seek(startPos);
                    byte[] b = new byte[1024];
                    int len = -1;
                    while ((len = in.read(b)) != -1) {
                        out.write(b, 0, len);
                    }
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章