package com.zad.lesson03;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//练习thead
public class TestThead2 extends Thread {
private final String url;
private final String name;
public TestThead2(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) {
TestThead2 t1 = new TestThead2("http://top.zhizhuyun.com.cn/static/admin/images/login-bg-top.jpg", "1.jpg");
TestThead2 t2 = new TestThead2("http://bs.zhizhuyun.com.cn/app/bashi-qrcode-manager.png", "2.jpg");
TestThead2 t3 = new TestThead2("http://bs.zhizhuyun.com.cn/app/bashi-qrcode-team.png", "3.jpg");
t1.start();
t2.start();
t3.start();
}
}
class WebDownloader {
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 方法异常");
}
}
}
下载结果
-
去网上下载 commons-io-1.4.jar包
-
创建一个线程类
-
调用这个commons-io中的下载图片方法
FileUtils.copyURLToFile(new URL(url), new File(name));
-
多线程进行图片下载



