java编程图片下载器
程序员文章站
2022-04-26 15:42:43
...
下载工具类
https://commons.apache.org/proper/commons-io/download_io.cgi
导入工具类
实现网图下载
package com.chen.lesson01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class Demo02 extends Thread {
private String name;
private String url;
public Demo02(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("下载了一张图片");
}
public static void main(String[] args) {
//地址,图片名称
Demo02 demo02 = new Demo02("https://img-blog.csdnimg.cn/20201106173014928.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81MTMyNTkxMg==,size_16,color_FFFFFF,t_70#pic_center","2.jpg");
demo02.start();
}
}
//下载器
class WebDownloader{
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
}
}
}
[
下载成功!