多线程分段下载

package com.zhbcm.down;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2021/7/29 22:19
 */
public class MultiThreadDownload
{
    public static String path = "http://localhost:8080/files/2021-07-31/9ee8571b-3465-4799-9fef-a176984d2568.msi"; // 要下载的网络资源文件路径
    public static int threadCount = 10; // 开启的线程数
    public static int runningThread = 10; // 记录已经运行的线程数量
    public static long startTime;

    private static final String filePath = "D:\\jenkins.msi"; //文件存放本地的路径

    public static void main(String[] args) throws Exception
    {
        startTime = System.currentTimeMillis();
        // 1.连接服务器,获取一个文件,获取文件的长度,在本地创建一个跟服务器一样大小的临时文件
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        int code = conn.getResponseCode();
        if (code == 200)
        {
            // 服务器端返回的数据的长度,实际上就是文件的长度
            long length = conn.getContentLength();
            System.out.println("文件总长度:" + length);
            // 在客户端本地创建出来一个大小跟服务器端一样大小的临时文件
            RandomAccessFile raf = new RandomAccessFile(filePath, "rwd");
            // 指定创建的这个文件的长度
            raf.setLength(length);
            raf.close();
            // 假设是3个线程去下载资源。
            // 平均每一个线程下载的文件大小.
            long blockSize = length / threadCount;
            for (int threadId = 1; threadId <= threadCount; threadId++)
            {
                // 第一个线程下载的开始位置
                long startIndex = (threadId - 1) * blockSize;
                long endIndex = threadId * blockSize - 1;
                if (threadId == threadCount)
                {
                    // 最后一个线程下载的长度要稍微长一点
                    endIndex = length;
                }
                System.out.println("线程:" + threadId + "下载:---" + startIndex + "--->" + endIndex);
                new DownLoadThread(threadId, startIndex, endIndex).start();
            }
        } else
        {
            System.err.println("服务器错误!");
        }
    }

    public static class DownLoadThread extends Thread
    {
        private int threadId;
        private long startIndex;
        private long endIndex;
        private InputStream is;
        private RandomAccessFile raf;


        /**
         * @param threadId   线程Id
         * @param startIndex 线程下载的开始位置
         * @param endIndex   线程下载的结束位置
         */
        public DownLoadThread(int threadId, long startIndex, long endIndex)
        {
            super();
            this.threadId = threadId;
            this.startIndex = startIndex;
            this.endIndex = endIndex;
        }

        @Override
        public void run()
        {
            try
            {
                long threadLength = endIndex - startIndex;
                long chunkLength = 1024 * 1024 * 5;
                double ceil = Math.ceil(threadLength / chunkLength);
                if (threadLength < chunkLength) ceil = 1.0;
                for (long i = 0; i < ceil; i++)
                {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setRequestMethod("GET");

                    long threadChunkLength = chunkLength * i + startIndex;
                    if (i != ceil - 1)
                    {
                        // 重要:请求服务器下载部分文件 指定文件的位置
                        conn.setRequestProperty("Range", "bytes=" + threadChunkLength + "-" + (threadChunkLength + chunkLength));
                    } else
                    {
                        conn.setRequestProperty("Range", "bytes=" + threadChunkLength + "-" + endIndex);
                    }
                    is = conn.getInputStream();// 已经设置了请求的位置,返回的是当前位置对应的文件的输入流
                    raf = new RandomAccessFile(filePath, "rwd");
                    // 随机写文件的时候从哪个位置开始写
                    raf.seek(threadChunkLength);// 定位文件

                    int len = 0;
                    byte[] buffer = new byte[2048];
                    while ((len = is.read(buffer)) != -1)
                    {
                        raf.write(buffer, 0, len);
                    }
                    System.out.println("线程:" + threadId + "下载完毕");
                }
                System.out.println("共计用时:" + (System.currentTimeMillis() - startTime) + "S");
            } catch (Exception e)
            {
                e.printStackTrace();
            } finally
            {
                runningThread--;
                if (runningThread == 0)
                {
                    // 所有的线程执行完毕
                    System.out.println("文件全部下载完毕!");
                }
                if (raf != null)
                {
                    try
                    {
                        raf.close();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                if (is != null)
                {
                    try
                    {
                        is.close();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值