多线程下载图片实例

2021-05-10 16:30

阅读:657

标签:下载图片   http   tst   comm   imp   stat   over   thread   commons   

package duoxiancheng;

import org.apache.commons.io.FileUtils;

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

public class Downloader  extends Thread{
    
    String url;
    String name;
    @Override
    public void run() {
        Downnn downnn = new Downnn();
        downnn.dodd(url,name);
        System.out.println("下载:"+url+" name:"+name);
    }
    public Downloader(String url,String name){
        this.url=url;
        this.name=name;
    }

    public static void main(String[] args) {
        Downloader downloader=new Downloader("https://n.sinaimg.cn/fashion/590/w240h350/20200618/5242-ivffpcs0137589.jpg","1.jpg");
        Downloader downloader1=new Downloader("https://n.sinaimg.cn/fashion/590/w240h350/20200618/5242-ivffpcs0137589.jpg","2.jpg");
        Downloader downloader2=new Downloader("https://n.sinaimg.cn/fashion/590/w240h350/20200618/5242-ivffpcs0137589.jpg","3.jpg");
        downloader.start();
        downloader1.start();
        downloader2.start();

    }
}

class Downnn{
    public void dodd(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

多线程下载图片实例

标签:下载图片   http   tst   comm   imp   stat   over   thread   commons   

原文地址:https://www.cnblogs.com/itzyz/p/13169771.html


评论


亲,登录后才可以留言!