package F_多线程.下载图片;
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;
public Downloader(String url, String name){
this.name=name;
this.url=url;
}
public Downloader() {
}
@Override
public void run() {
WedDownloader ann=new WedDownloader();
try {
ann.downloader(url,name);
System.out.println("图片姓名"+name);
} catch (IOException e) {
e.printStackTrace();
System.out.println("下载失败");
}
}
public static void main(String[] args) {
Downloader bnn=new Downloader("https://i-blog.csdnimg.cn/blog_migrate/b6a192ff38df9cc3636928f56a3bf45b.jpeg#pic_center","图片1");
Downloader cnn=new Downloader("https://i-blog.csdnimg.cn/blog_migrate/b6a192ff38df9cc3636928f56a3bf45b.jpeg#pic_center","图片2");
Downloader dnn=new Downloader("https://i-blog.csdnimg.cn/blog_migrate/b6a192ff38df9cc3636928f56a3bf45b.jpeg#pic_center","图片3");
new Thread(bnn).start();
new Thread(cnn).start();
new Thread(dnn).start();
}
}
//文件下载工具类
class WedDownloader{
//远程路径,存储名字
public void downloader(String url,String name) throws IOException {
FileUtils.copyURLToFile(new URL(url),new File(name));
}
}
io流工具包下图片测试
最新推荐文章于 2024-07-15 10:09:52 发布