【JAVA 多线程下载】

  1. 单线程,实现具体任务
package com.hongfu.spring.Thread;

import java.io.File;
import java.io.FileNotFoundException;
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 SingleThreadFileDownload implements Runnable{
    // 下载路径
    private String urlDown;

    // 本地存放路径
    private String filePath;
    //文件开始地址
    private long start;
    //文件结束地址
    private long end;

    public SingleThreadFileDownload(String urlDown, String filePath, long start, long end) {
        this.urlDown = urlDown;
        this.filePath = filePath;
        this.start = start;
        this.end = end;
    }
    @Override
    public void run() {
        InputStream is = null;
        RandomAccessFile out = null;
        try {
            //获取下载的部分
            URL url = new URL(urlDown);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Range", "bytes=" + start + "-" + end);

            File file = new File(filePath);
            out = null;
            if (file != null) {
                out = new RandomAccessFile(file, "rw");
            }
            out.seek(start);

            is = conn.getInputStream();
            byte[] bytes = new byte[1024];
            int l = 0;
            while ((l = is.read(bytes))!=-1){
                out.write(bytes,0,l);
            }
            System.out.println(Thread.currentThread().getName()+"完成下载!");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  1. 创建单线程,实现多线程下载
package com.hongfu.spring.Thread;

import java.net.URL;
import java.net.URLConnection;

public class moreThreadDownFile {

    public static void main(String[] args) {
        try {
            getFileWithThreadPool("https://issuecdn.baidupcs.com/issue/netdisk/yunguanjia/BaiduNetdisk_7.8.5.1.exe","D:\\百度.exe",5);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void getFileWithThreadPool(String urlLocation, String filePath, int poolLength) throws Exception{
        long start = 0;
        int length = 0;
        URL url = null;
        url = new URL(urlLocation);
        URLConnection urlConnection = url.openConnection();
        //文件长度
        length = urlConnection.getContentLength();
        System.out.println(length);
        for (int i = 0; i < poolLength; i++) {
            start = i * length / poolLength;
            long end = (i + 1) * length / poolLength - 1;
            System.out.println(start+"---------------"+end);

            Thread thread = new Thread(new SingleThreadFileDownload(urlLocation, filePath, start, end));
            thread.setName("线程"+(i+1));
            thread.start();
        }

    }
}

  1. 测试结果如图
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值