package com.Java;

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 thread1 = new TestThread2("https://www.kuangstudy.com/assert/course/c1/01.jpg","img1.jpg");
TestThread2 thread2 = new TestThread2("https://www.kuangstudy.com/assert/course/c1/02.jpg","img2.jpg");
TestThread2 thread3 = new TestThread2("https://www.kuangstudy.com/assert/course/c1/03.jpg","img3.jpg");
// 因为多线程 所以是一起执行 并非t1t2t3
thread1.start();
thread2.start();
thread3.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方法出现了问题");
}
}
}
}