1.Runnable实现根据URL 下载图片
依赖jar包
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
package com.kuang.test;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* @Auther: 18352
* @Date: 2021/7/25 17:51
* @Description: TODO
*/
public class TestTheead implements Runnable{
private String url;
private String name;
public TestTheead(String url, String name) {
this.url = url;
this.name = name;
}
@Override
public void run() {
DownFile downFile = new DownFile();
downFile.cocyurl(url, name);
System.out.println("下载完成"+name);
}
public static void main(String[] args) {
TestTheead testTheead = new TestTheead("https://avatar.csdnimg.cn/C/D/0/3_zixiangli.jpg","zixiangli.jpg");
TestTheead testTheead2 = new TestTheead("https://avatar.csdnimg.cn/C/D/0/3_zixiangli.jpg","zixiangli.jpg");
TestTheead testTheead3 = new TestTheead("https://avatar.csdnimg.cn/C/D/0/3_zixiangli.jpg","zixiangli.jpg");
Thread thread1 = new Thread(testTheead);
Thread thread2 = new Thread(testTheead2);
Thread thread3 = new Thread(testTheead3);
thread1.start();
thread2.start();
thread3.start();
}
}
class DownFile{
public void cocyurl(String url, String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
}
}
}