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. AB串(上帝都不会,我就没救了)

    [题目分析] 设答案的长度为m,

  2. Java多个数字求和输出

    设计思想: 首先为了能够输入数字,先引入一个类import java.util.Scanner;因为是多个数字求和,所以为了方便声明一个数组double[] num;再声明一个变量int number ...

  3. 建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。

    package exception; public class TestException { public static void main(String[] args) { for(int i=0 ...

  4. [转]Unity3d之MonoBehaviour的可重写函数整理

    最近在学习Unity3d的知识.虽然有很多资料都有记录了,可是我为了以后自己复习的时候方便就记录下来吧!下面的这些函数在Unity3d程序开发中具有很重要的作用. Update 当MonoBehavi ...

  5. java传递和返回对象

    java传递的只是一个引用,一定要注意准确认识在对象传递和赋值时所发生的一切. 事实上,java中的每个对象(除了基本数据类型以外)的标识符都属于指针的一种,但是其使用受到了严格的限制和防范,不仅在编 ...

  6. ABAP 内表 详解

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

  7. CUBRID学习笔记 44 UPDATE 触发器 更新多表 教程

    cubrid的中sql查询语法UPDATE c#,net,cubrid,教程,学习,笔记欢迎转载 ,转载时请保留作者信息.本文版权归本人所有,如有任何问题,请与我联系wang2650@sohu.com ...

  8. git 简单用

    之前一直用svn,用git也只是从github上clone代码.现在自己试试用git做版本控制.等用熟悉了,再看看怎么搞分支,合并,多人开发之类的. 1下载个git客户端,如Git for Windo ...

  9. POJ 1321棋盘问题

    题意: 给定一个n*n(n<=8)的棋盘, 有些地方能放棋子, 有些不能. 放m个棋子, 求能使这m个棋子不同行且不同列的方案数. 分析: 用一个一维数组标记行和列, 深搜一下. #includ ...

  10. 使用xml来显示获取的mysql数据

    mysql test -u test -X -e 'select * from employees where empid = 1' 其中 -X 就是以xml形式显示