项目目录结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cddb8ebadf7292a7bec0941dc3a0bca1.png)
步骤
导入common-io jar包
- 下载链接
- 将jar包复制到lib中,并将lib包添加为库
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1de6a3f8bbb30f2db87b3b76b72099a0.png)
编写工具类WebDownloader
public class WebDownloader {
public void downloader(String url,String name){
try{
FileUtils.copyURLToFile(new URL(url),new File(name));
}catch (IOException e){
e.printStackTrace();
System.out.println("文件下载失败!");
}
}
}
编写网图下载线程类
public class DownloadPictureThread extends Thread{
private String url;
private String name;
public DownloadPictureThread(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(name);
}
}
编写程序入口类Application
- 注:自行添加网图url测试——尽量保证网图大小相近,便于多次测试观察出线程存在
public class Application {
public static void main(String[] args) {
DownloadPictureThread t1 = new DownloadPictureThread("url1", "1.jpg");
DownloadPictureThread t2 = new DownloadPictureThread("url2", "2.jpg");
DownloadPictureThread t3 = new DownloadPictureThread("url3", "3.jpg");
t1.start();
t3.start();
t2.start();
}
}