多线程下载图片java

package com.ljy.test;

import org.apache.commons.io.FileUtils;

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

public class TestThread extends Thread{
    private String name;
    private  String url;

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

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

    public static void main(String[] args) {
        TestThread t1 = new TestThread("https://tse3-mm.cn.bing.net/th/id/OIP-C.owpbNjuu23xkyc4yrX9BDQHaEo?w=247&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7","1.jpg");
        TestThread t2 = new TestThread("https://pic3.zhimg.com/v2-58d652598269710fa67ec8d1c88d8f03_r.jpg?source=1940ef5c","2.jpg");
        TestThread t3 = new TestThread("https://pic3.zhimg.com/v2-c6ae9c3aff36b9b221258f6a90577902_r.jpg","3.jpg");

        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方法出问题!!!");
        }
    }
}

多线程下载图片java_第1张图片

 

你可能感兴趣的:(自学java,java,前端,开发语言)