栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA 多线程下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 多线程下载

单线程下载

package com.autumn.lesson04;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class FileDownload implements Runnable{

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

    public FileDownload(String urlLocation, String filePath, long start, long end) {
        this.urlLocation = urlLocation;
        this.filePath = filePath;
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        InputStream is = null;
        RandomAccessFile out = null;
        try {
            //获取下载的部分
            URL url = new URL(urlLocation);
            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();
            }
        }

    }
}

实现多线程下载

package com.autumn.lesson04;

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


public class Controller {

    public 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 FileDownload(urlLocation, filePath, start, end));
                thread.setName("线程"+(i+1));
                thread.start();
            }

    }

}

测试

package com.autumn.lesson04;


public class Test {
    public static void main(String[] args) throws Exception{
        Controller controller = new Controller();
        controller.getFileWithThreadPool("https://issuecdn.baidupcs.com/issue/netdisk/yunguanjia/BaiduNetdisk_7.8.5.1.exe","D:\autumn\Desktop\aaa\百度.exe",5);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425346.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号