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();
    }
   }
  }
 }
}

java多线程下载网络图片的更多相关文章

  1. JAVA多线程下载网络文件

    JAVA多线程下载网络文件,开启多个线程,同时下载网络文件.   源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...

  2. java多线程下载和断点续传

    java多线程下载和断点续传,示例代码只实现了多线程,断点只做了介绍.但是实际测试结果不是很理想,不知道是哪里出了问题.所以贴上来请高手修正. [Java]代码 import java.io.File ...

  3. java 多线程下载功能

    import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; impo ...

  4. java 多线程下载文件 以及URLConnection和HttpURLConnection的区别

    使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...

  5. Java多线程下载文件

    package com.test.download;   import java.io.File; import java.io.InputStream; import java.io.RandomA ...

  6. Java多线程下载器FileDownloader(支持断点续传、代理等功能)

    前言 在我的任务清单中,很早就有了一个文件下载器,但一直忙着没空去写.最近刚好放假,便抽了些时间完成了下文中的这个下载器. 介绍 同样的,还是先上效果图吧. Jar包地址位于 FileDownload ...

  7. Java多线程下载初试

    一.服务端/客户端代码的实现 服务端配置config @ConfigurationProperties("storage") public class StoragePropert ...

  8. Java多线程下载分析

    为什么要多线程下载 俗话说要以终为始,那么我们首先要明确多线程下载的目标是什么,不外乎是为了更快的下载文件.那么问题来了,多线程下载文件相比于单线程是不是更快? 对于这个问题可以看下图. 横坐标是线程 ...

  9. java 多线程下载文件并实时计算下载百分比(断点续传)

    多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...

随机推荐

  1. HDU 1004 Let the Balloon Rise map

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. centos6.5 ssh安全优化,修改默认端口名,禁止root远程登录

    一.修改默认端口号 第一步: vi /etc/sysconfig/iptables 添加修改后的端口号的配置 -A INPUT -p tcp -m state --state NEW -m tcp - ...

  3. Spring 框架 详解 (三)-----IOC装配Bean

    IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...

  4. 01.AFNetworking原理及常用操作

    AFN的六大模块 NSURLConnection,主要对NSURLConnection进行了进一步的封装,包含以下核心的类: AFURLConnectionOperation AFHTTPReques ...

  5. VBA中自定义类和事件的(伪)注册

    想了解一下VBA中自定义类和事件,以及注册事件处理程序的方法. 折腾了大半天,觉得这样的方式实在称不上“注册”,所以加一个“伪”字.纯粹是瞎试,原理也还没有摸透.先留着,有时间再接着摸. 做以下尝试: ...

  6. gastic 安装

    所有文件下载地址: ftp://ftp.broadinstitute.org/pub/GISTIC2.0/ cd /home/software/ tar zxf GISTIC_2_0_22.tar.g ...

  7. ALV详解:Function ALV(一)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  8. [Effective Java]第二章 创建和销毁对象

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. C运行时的数据结构

    本文描述一下:C运行时的数据结构,相关的段,压栈等 unix默认的编译器会将编译生成的文件默认命名为a.out 目标文件和可执行文件可以有几种不同的格式,所有这些不同格式具有一个共同的概念,那就是段. ...

  10. Switch用法

    package com.cz.test; public class SwitchExample1 { /** * @param args */ public static void main(Stri ...