package com.sean.base.threadStudy;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* 练习Thread,实现多线程同步下载图片
* @create 2021-02-27 11:06
*/
public class Demo02 extends Thread{
private String url;
private String name;
public Demo02(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) {
Demo02 d1 = new Demo02("https://wx2.sinaimg.cn/mw690/002mNrIZly1go19kmrox7j60ku0qvdlv02.jpg","20.jpg");
Demo02 d2 = new Demo02("https://wx3.sinaimg.cn/mw690/006fdnrmgy1gnzyl0pmbyj31gi13dtm8.jpg","21.jpg");
Demo02 d3 = new Demo02("https://wx2.sinaimg.cn/mw690/001UxQLbly1go15bowahwj60uk0zwgui02.jpg","22.jpg");
d1.start();
d2.start();
d3.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异常,download方法出现");
}
}
}