基于HTTP协议的多线程下载实现思路

基于HTTP协议的多线程下载,实现思路如下:
1.通过URL实例获取HTTPConnection;
2.根据HTTPConnection获取文件大小,通过文件大小,设置的线程数,计算每一个现场下载文件的起止Byte数。
3.在每个线程里,通过connection.setRequestProperty设置获取文件的Byte的起止位置。
4.线程中,获取到下载文件后,先在本地创建临时文件,通过seek方法设置文件偏置点,然后将下载文件流write到临时文件。


代码实例:


    package com.zhang;

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

    public class MutileThreadDownload1 {
        /*线程数*/
        private static int threadCount = 30;
        /*区块大小*/
        private static long blockSize;

        public static void main(String[] args) {
            /*服务器下载文件的路径*/
            String path1 = "http://127.0.0.1:8090/1.avi";
            String path = "http://127.0.0.1:8090/1.avi";
            try {
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                int code = connection.getResponseCode();
                if (code == 200) {
                    long size = connection.getContentLength();
                    System.out.println("需要下载的文件大小:" + size);
                    blockSize = size / threadCount;
                    // 1,在本地创建和服务器一样大小的空白文件

                    /*File file = new File("temp.exe");
                    RandomAccessFile raf = new RandomAccessFile(file, "rw");
                    raf.setLength(size);*/

                    /*开启若干线程,分别下载对应的资源*/
                    for (int i = 1; i <= threadCount; i++) {
                        System.out.println("开启线程" + i);
                        long startIndex = ((i - 1) * blockSize + 0);
                        long endIndex = blockSize * i - 1;
                        if (i == threadCount) {
                            /*
                             * 最后一个线程
                             * */
                            endIndex = size - 1;
                        }
                        new DownLoadThread(i, startIndex, endIndex, path).start();
                        System.out.println("下载的位置:" + startIndex + "~" + endIndex);

                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    class DownLoadThread extends Thread {
        private int threadId;
        private long startIndex;
        private long endIndex;
        private String path;

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

        public void run() {
            try {
                URL url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
                // connection.setRequestProperty("Range", "bytes=" + startIndex +
                // "-" + endIndex);
                connection.setConnectTimeout(5000);
                int code = connection.getResponseCode();
                System.out.println("code" + code);

                InputStream is = connection.getInputStream();
                File file = new File("temp.avi");
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                raf.seek(startIndex);
                System.out.println("第" + threadId + "个线程开始写文件的位置:" + String.valueOf(startIndex));
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    raf.write(buffer, 0, len);
                }
                is.close();
                raf.close();
                System.out.println("线程" + threadId + "下载完毕");
                if (code / 100 == 2) {

                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件工程师文艺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值