网图下载
import org.apache.commons.io.FileUtils;标签:String,url,name,new,网图,public,下载,TestThread2 From: https://www.cnblogs.com/yuan947022015/p/17653658.html
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://img0.baidu.com/it/u=3021883569,1259262591&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1692982800&t=5858b3800995bb2c1af459caaab34932", "1.jpg");
TestThread2 t2 = new TestThread2("https://img0.baidu.com/it/u=3021883569,1259262591&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1692982800&t=5858b3800995bb2c1af459caaab34932", "2.jpg");
TestThread2 t3 = new TestThread2("https://img0.baidu.com/it/u=3021883569,1259262591&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1692982800&t=5858b3800995bb2c1af459caaab34932", "3.jpg");
t1.start();
t2.start();
t3.start();
}
public static 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出现问题");
}
}
}
// 下载器
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出现问题");
}
}
}