//网图下载
package com.Bubbles.Demo1;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
//练习Thread,实现多线程同步下载图片
public class Testgread2 extends Thread {
private String url; //网络图片地址
private String name; //保存的文件名
public Testgread2(String url,String name){
this.url = url;
this.name = name;
}
//描写重写run方法
//下载图片线程的执行体
@Override
public void run() {
webDownloader webDownloader = new webDownloader ();
webDownloader.downdarerd (url,name);
//打印
System.out.println ("下载文件名为"+name);
}
public static void main(String[] args) {
//创建对象
Testgread2 t1 = new Testgread2 ("https://pic32.photophoto.cn/20140825/0005018319994720_b.jpg","1.jpg");
Testgread2 t2 = new Testgread2 ("https://img2.woyaogexing.com/2018/01/11/c9fbaa101c07e288!600x600.jpg","2.jpg");
Testgread2 t3 = new Testgread2 ("http://pic17.nipic.com/20111101/8726441_123115198198_2.jpg","3.jpg");
//启动
t1.start ();
t2.start ();
t3.start ();
}
}
//下载器
class webDownloader {
//下载方法
//FileUtils文件工具类
public void downdarerd(String url,String name){
try {
FileUtils.copyURLToFile (new URL (url), new File (name));
} catch (IOException e) {
e.printStackTrace ();
System.out.println ("IO异常,downdarerd方法出现问题");
}
}
}
--作者:Bubbles