多线程文件下载 - 数组切分,截取文件名称

java 复制代码
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestThread2 extends Thread {
    private List<String> urlList;

    public TestThread2() {
    }

    public TestThread2(List<String> urlList) {
        this.urlList = urlList;
    }

    @Override
    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        urlList.forEach(x -> {
            try {
                System.out.println(Thread.currentThread().getName() + "执行");
                webDownLoader.downloader(x);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("https://pic.xxxxx-cdn.com//1/130702.jpg",
"https://pic.xxxxx-cdn.com//1/106152.jpg",
"https://pic.xxxxx-cdn.com//1/100603.jpg",
"https://pic.xxxxx-cdn.com//1/98446.jpg",
"https://pic.xxxxx-cdn.com//1/98436.jpg",
"https://pic.xxxxx-cdn.com//1/98411.jpg",
"https://pic.xxxxx-cdn.com//1/98176.jpg",
"https://pic.xxxxx-cdn.com//1/98072.jpg");
        //切分数组
        List<List<String>> lists = splitList(list, 3);
        lists.forEach(x -> {
            TestThread2 td = new TestThread2(x);
            td.start();
        });

    }

    /**
     * @param list    数组
     * @param partNum 切分次数
     * @return 先按平均值切分,最后一次全部
     */
    private static List<List<String>> splitList(List<String> list, int partNum) {
        List<List<String>> rList = new ArrayList<>();
        int avgNum = list.size() / partNum;
        System.out.println(avgNum + ":平均数");

        for (int part = 0; part < partNum; part++) {
            //判断start起始位置 0 : avgNum * part
            int start = 0 == part ? 0 : avgNum * part;
            //判断最后一次结束位置 list.size() : start + avgNum
            int end = partNum == part + 1 ? list.size() : start + avgNum;
            System.out.println(String.format("第%s次,起始:%s 位置:%s", part + 1, start, end));
            List<String> subList = new ArrayList<>();
            for (int i = start; i < end; i++) {
//                System.out.println(list.get(i));
                subList.add(list.get(i));
            }
            rList.add(subList);
        }
        System.out.println(rList);
        return rList;
    }
}

class WebDownLoader {
    public void downloader(String url) throws IOException {
class WebDownLoader {
    public void downloader(String url) throws IOException {
        // 提取文件名
        String fileName = url.substring(url.lastIndexOf('/') + 1); // "filename.txt"
        // 提取名称部分(例如去掉扩展名)
        String name = fileName.substring(0, fileName.lastIndexOf('.')); // "filename"
        FileUtils.copyURLToFile(new URL(url), new File(fileName));
    }
}
        FileUtils.copyURLToFile(new URL(url), new File(fileName));
    }
}
相关推荐
Wang's Blog35 分钟前
Java 项目实战: 交友项目 - 项目:需求分析与滑动数据设计
java·开发语言·交友
前端 贾公子1 小时前
第 5 章:Codex 从可运行程序到可交付工具
java·前端·数据库
CoderYanger1 小时前
Java EE 进阶:5. Spring IoC&DI(第三弹):Spring DI
java·开发语言·spring boot·spring·面试·java-ee·mvc
一条泥憨鱼1 小时前
【Kafka】零基础学习教程
java·分布式·学习·kafka
Wang's Blog2 小时前
Java 项目实战:交友项目 - 即时通信总结:全链路复盘、可复用清单与自检
java·python·交友
文丑颜不良啊2 小时前
LeetCode1501~1520题目解析
java·leetcode
长不大的杨瑞杰2 小时前
OpenCode v2 LSP 实战:内置语言服务器、自定义配置与诊断闭环
java·人工智能·typescript·lsp·opencode
云和数据.ChenGuang2 小时前
python和java的try
java·开发语言·python
日日大王学编程2 小时前
阿里巴巴开源 Data Agent(Text2SQL)项目全维度详解
java·ai·开源
Wang's Blog2 小时前
Java 项目实战:交友项目 - 交友左滑右滑
java·交友