ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

多线程.网图下载

2022-08-08 21:01:29  阅读:213  来源: 互联网

标签:String url TestThread2 2458894 网图 new 多线程 下载 name


package oop.dxc;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//练习Thread,实现多线程同步下载图片
public class TestThread2 extends Thread{

    private String url;  //网络图片地址
    private String name;  //保存的文件名

    public TestThread2(String url,String name){
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downloader(url,name);
        System.out.println("下载了文件名为:"+name);
    }

    public static void main(String[] args) {
            TestThread2 t1 = new TestThread2("https://www.icode9.com/i/l/?n=22&i=blog/2458894/202203/2458894-20220305072938762-1726173585.png","1.png");
            TestThread2 t2 = new TestThread2("https://www.icode9.com/i/l/?n=22&i=blog/2458894/202203/2458894-20220305074807533-1092546476.png","2.png");
            TestThread2 t3 = new TestThread2("https://www.icode9.com/i/l/?n=22&i=blog/2458894/202203/2458894-20220305075846256-1824775882.png","3.png");

            t1.start();
            t2.start();
            t3.start();
    }
}


//下载器
class WebDownLoader{
    //下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO异常,downloader方法出现问题");
        }

    }
}

 

标签:String,url,TestThread2,2458894,网图,new,多线程,下载,name
来源: https://www.cnblogs.com/sususyq-/p/16563395.html

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有