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

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));
    }
}
相关推荐
工业甲酰苯胺20 小时前
实现 json path 来评估函数式解析器的损耗
java·前端·json
老前端的功夫20 小时前
Web应用的永生之术:PWA落地与实践深度指南
java·开发语言·前端·javascript·css·node.js
@forever@20 小时前
【JAVA】LinkedList与链表
java·python·链表
LilySesy20 小时前
ABAP+WHERE字段长度不一致报错解决
java·前端·javascript·bug·sap·abap·alv
六件套是我20 小时前
redission实现延时队列
android·java·servlet
王元_SmallA21 小时前
Redis Desktop Manager(Redis可视化工具)安装
java·后端
ᐇ95921 小时前
Java HashMap深度解析:数据结构、原理与实战指南
java·开发语言·数据结构
好好研究21 小时前
Spring框架 - 开发方式
java·后端·spring
武子康21 小时前
Java-166 Neo4j 安装与最小闭环 | 10 分钟跑通 + 远程访问 Docker neo4j.conf
java·数据库·sql·docker·系统架构·nosql·neo4j
2301_796512521 天前
Rust编程学习 - 为什么说Cow 代表的是Copy-On-Write, 即“写时复制技术”,它是一种高效的 资源管理手段
java·学习·rust