多线程继承Thread类
-
package com.lening.thread;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @Author WangEn
* @CreateTime: 2021-04-07-11-29
* @Emile: wen_5988@163.com
*/
public class TestThread extends Thread {
private String url;
private String name;
public TestThread(String url, String name) {
this.url = url;
this.name = name;
}
@SneakyThrows
@Override
public void run() {
new UpDownLoad().upDownLoad(url,name);
System.out.println("下载的图片名字是"+name);
}
public static void main(String[] args) {
new TestThread("https://img1.baidu.com/it/u=1485012388,2380514454&fm=26&fmt=auto&gp=0.jpg","地球.jpg").start();
new TestThread("https://img2.baidu.com/it/u=3355464299,584008140&fm=26&fmt=auto&gp=0.jpg","A梦.jpg").start();
new TestThread("https://img1.baidu.com/it/u=2496571732,442429806&fm=26&fmt=auto&gp=0.jpg","水.jpg").start();
}
}
/**
* 下图工具类
*/
class UpDownLoad{
public void upDownLoad(String url,String name) throws MalformedURLException {
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("图片下载失败");
}
}
}
2.FileUtils是api开发下面的一个下载工具包
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>