//练习Thread,实现多线程同步下载图片 public class TestThread2 extends Thread{ private String url;//网络图片位置 private String name;//保存的文件名 public TestThread2(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); } public static void main(String[] args) { TestThread2 testThread1 = new TestThread2("https://dfzximg01.dftoutiao.com/news/20211205/20211205102621_b0ba0f3030f3535b245595198daea04a_1.jpeg","1.jpg"); TestThread2 testThread2 = new TestThread2("https://dfzximg01.dftoutiao.com/news/20211205/20211205102621_b0ba0f3030f3535b245595198daea04a_2.jpeg","2.jpg"); TestThread2 testThread3 = new TestThread2("https://dfzximg01.dftoutiao.com/news/20211205/20211205102621_b0ba0f3030f3535b245595198daea04a_3.jpeg","3.jpg"); testThread1.start(); testThread2.start(); testThread3.start(); } } //下载器 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("IO异常,downloader方法出现问题"); } } }
网图下载1
最新推荐文章于 2024-07-21 22:27:18 发布