• 多线程例子:网图下载


    多线程例子:网图下载

    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    
    //练习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 t1 = new TestThread2("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594717080876&di=fe083e5f436c12d673004789fa9c9398&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg","1.jpg");
            TestThread2 t2 = new TestThread2("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594717290398&di=e0c6f6d72e6678a887b672793129c24e&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F56%2F12%2F01300000164151121576126282411.jpg","2.jpg");
            TestThread2 t3 = new TestThread2("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594717332850&di=63815e0cd3207db1c64dab607ecff742&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F52%2F52%2F01200000169026136208529565374.jpg","3.jpg");
    
            //多线程,每次都不一样
            t1.start();
            t2.start();
            t3.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方法出现问题");
            }
        }
    }
    
  • 相关阅读:
    python XML实例
    python XML基础
    python 使用正则表达式的爬虫
    python cookies 爬虫处理
    Selenium 2自动化测试实战22(处理HTML5的视频播放)
    Selenium 2自动化测试实战21【调用JavaScript(控制浏览器的滚动条)】
    Selenium 2自动化测试实战20(操作cookie)
    Selenium 2自动化测试实战19(下载文件)
    Selenium 2自动化测试实战18(上传文件)
    Selenium 2自动化测试实战17(警告框处理)
  • 原文地址:https://www.cnblogs.com/helloxiaolu/p/13298882.html
Copyright © 2020-2023  润新知