网图下载(理解多线程)

2021-06-06 06:04

阅读:398

标签:catch   bing   package   commons   下载   nload   keyword   head   amp   

网图下载

    package www.shushu.Thread;
?
import org.apache.commons.io.FileUtils;
?
import java.io.File;
import java.io.IOException;
import java.net.URL;
?
public class TestThread1 extends Thread{
   private  String url;//网络图片地址
   private  String name;//保存的文件名为
   public TestThread1(String url,String name){
       this.url = url;
       this.name = name;
  }
   @Override
   //下载图片线程的执行体
   public void run() {
       WebDownloader webDownloader = new WebDownloader();
       try {
           webDownloader.downloader(url,name);
           System.out.println("下载了文件名为:"+name);
      } catch (IOException e) {
           e.printStackTrace();
      }
  }
?
   public static void main(String[] args) {
       TestThread1 testThread1 = new TestThread1("https://tse3-mm.cn.bing.net/th/id/OIP.qQJGowqkvmqvhsyV8aKpZQHaNK?w=115&h=186&c=7&o=5&dpr=1.25&pid=1.7","最好的我们1.jpg");
       TestThread1 testThread2 = new TestThread1("https://tse4-mm.cn.bing.net/th/id/OIP.ByP8_LdBun-6F-J8hpyPpwHaE8?w=279&h=186&c=7&o=5&dpr=1.25&pid=1.7","最好的我们2.jpg");
       TestThread1 testThread3 = new TestThread1("https://tse2-mm.cn.bing.net/th/id/OIP.AflXgm85IBzU5R4cLeeTWwHaE3?w=284&h=186&c=7&o=5&dpr=1.25&pid=1.7","最好的我们3.jpg");
       testThread1.start();;
       testThread2.start();
       testThread3.start();
  }
}
class WebDownloader{
   public void downloader(String url,String name) throws IOException {
       FileUtils.copyURLToFile(new URL(url),new File(name));
  }
}

 

网图下载(理解多线程)

标签:catch   bing   package   commons   下载   nload   keyword   head   amp   

原文地址:https://www.cnblogs.com/changhanbing/p/14616334.html


评论


亲,登录后才可以留言!