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. django cycle标签

    django 模板系统 有很多标签,其中cycle我觉得不好理解,至少网上文档也不好理解. 这些标签可以通过渲染模板文件而获得我们预期的效果和文字,常用的有如下这些标签: 标签:{% 标签名 %}{% ...

  2. VC如何让窗口半透明

    转载:http://blog.csdn.net/bichenggui/article/details/8291946 //加入WS_EX_LAYERED扩展属性 LONG styleValue = : ...

  3. mysql 用sql语句查询一个表中的所有字段类型、注释

    SELECT column_name,column_comment,data_type FROM information_schema.columns WHERE table_name='表名' AN ...

  4. 【leetcode❤python】Ransom Note

    #-*- coding: UTF-8 -*- class Solution(object):       def canConstruct(self, ransomNote, magazine):   ...

  5. FreeSWITCH第三方库(视频)的简单介绍(二)

    FreeSWITCH使用了大量的第三方库,本文档主要介绍视频相关库的信息: 音频相关库的信息介绍参考:http://www.cnblogs.com/yoyotl/p/5486753.html 其他相关 ...

  6. CANopen笔记2

    PDO 过程数据对象用于在节点之间传送过程数据,如I/O模块I/O状态读取和设定,模拟量采集和模拟量输出等等,协议考虑从机硬件限制最多支持4组PDO,每组包含一个RPDO和一个TPDO.The Gol ...

  7. MySQL用户名和密码问题

    MySQL使用脚本的方法: source d:\datafilename.sql # mysql -uroot -p Enter password: ERROR 1045 (28000): Acces ...

  8. sencha touch建立新项目

    首先你得有一个sencha touch的环境,如下图: 图1 touch的sdk环境 有了这个之后,通过在cmd中执行下列命令: sencha -sdk /path/to/framework gene ...

  9. JVM 1.类的加载、连接、初始化

    Java类的加载是由类加载器来完成的,过程如下: 首先,加载是把硬盘.网络.数据库等的class文件中的二进制数据加载到内存的过程,然后会在Java虚拟机的运行时数据区的堆区创建一个Class对象,用 ...

  10. Object-c : block需要注意的几点问题

    摘自:http://www.cnblogs.com/ltpblog/p/3684127.html Date : 2015-12-4 1. Block定义 1) 说明: a. Block是OC中的一种数 ...