zoukankan      html  css  js  c++  java
  • java多线程下载网络图片

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class ImageDown {
     /**
      * 下载图片
      * @param str
      */
     public static void downImage(String str) {
      BufferedInputStream input = null;
      BufferedOutputStream out = null;
      URL url;
      try {
       url = new URL(str);
       URLConnection conn;
       conn = url.openConnection();
       String[] strName = str.split("/");
       String imageName = strName[strName.length - 1]; // 图片名
       String imageUrl = "D:/" + imageName;
       InputStream in = conn.getInputStream();
       input = new BufferedInputStream(in);
       out = new BufferedOutputStream(new FileOutputStream(imageUrl));
       int len = 0;
       while ((len = input.read()) != -1) {
        out.write(len);
       }
      } catch (MalformedURLException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      } finally {
       if (out != null) {
        try {
         out.close();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }

       if (input != null) {
        try {
         input.close();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      }
     }
     
     
     
     /**
      * 获取路径
      * @return
      */
     public static ArrayList<String> getUrl(){
      ArrayList<String> list= new ArrayList<String>();
      BufferedReader read=null;
      try {
       URL url = new URL("http://www.tupianzj.com/chuangyi/");
       URLConnection conn = url.openConnection();
       InputStream input = conn.getInputStream();
       read = new BufferedReader(new InputStreamReader(input));//字符转换成字节
       //http://img.tupianzj.com/uploads/allimg/160525/9-1605251F6280-L.jpg
       Pattern pattern = Pattern.compile("http://\w+\.\w+\.com/\w+/\w+/\d+/[0-9]-(\w+\d+|\d+\w+)([-][A-Z]\.jpg|\.jpg)");
       String str=null;
       while((str=read.readLine())!=null){
        Matcher match = pattern.matcher(str);
        if (match.find()) {
         String imageUrl = match.group();
         list.add(imageUrl);
        }
       }
      } catch (MalformedURLException e) {
       e.printStackTrace();
      }catch (IOException e) {
       e.printStackTrace();
      }finally{
       if (read!=null) {
        try {
         read.close();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      }
      return list;
     }
    }

    /**
     * 开启多线程
     * @author Administrator
     *
     */
    class downimage extends Thread{
     String str;
     public downimage(String str){
      this.str=str;
     }
     @Override
     public void run() {
      BufferedInputStream input = null;
      BufferedOutputStream out = null;
      URL url;
      try {
       url = new URL(str);
       URLConnection conn;
       conn = url.openConnection();
       String[] strName = str.split("/");
       String imageName = strName[strName.length - 1]; // 图片名
       String imageUrl = "D:/" + imageName;
       InputStream in = conn.getInputStream();
       input = new BufferedInputStream(in);
       out = new BufferedOutputStream(new FileOutputStream(imageUrl));
       int len = 0;
       while ((len = input.read()) != -1) {
        out.write(len);
       }
      } catch (MalformedURLException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      } finally {
       if (out != null) {
        try {
         out.close();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
       if (input != null) {
        try {
         input.close();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      }
     }
    }

  • 相关阅读:
    在vim中设置将tab自动转化为4个空格
    nginx1.4.6+php5.5.11+mysql5.6.17+mecache+opcache
    Centos7安装杀毒软件ClamAV
    网页中meta标记
    js刷新页面方法大全
    微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法
    phpcms v9 如何实现用户登录
    web页面自适应手机屏幕宽度
    微信公共平台消息回复类
    自动回复微信消息
  • 原文地址:https://www.cnblogs.com/byteworld/p/5690670.html
Copyright © 2011-2022 走看看