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. 【Web】简谈如何监听浏览器的关闭

    > 参考的优秀文章 beforeunload实现关闭离开的提示 想起以前做的一个小系统,一个企业内部小型的测试系统,让考生在给定时间内完成考试,如果考生中退出,那么下次进来可以利用剩余的考试时间 ...

  2. 使用jquery构建Metro style 返回顶部

    个人一直对metro风格的东西情有独钟,偶然间在腾讯网看到一款小插件,蓝色Metro风格的,所以决定把它放到我的博客中,这样做应该不会有版权问题吧orz.. Complete code 后言 我把他原 ...

  3. 关于Android6.0之后的权限问题

    https://github.com/mylhyl/AndroidAcp AndroidAcp 使用: 加入 compile 'com.mylhyl:acp:1.1.7' PermisionUtils ...

  4. CSocket服务器(TCP)

    我的理解:把服务器和客户端的交互工程比喻成外来人员访问公司,每来一个客户端访问,需要服务器的前台经理接待此客户,然后前台经理呼叫一个接待员来将客户带上楼.服务器的两个角色前台经理和接待员就是服务器的两 ...

  5. Web自定义协议,BS端启动CS端,

    实例 1.准备CS项目,windows窗体应用程序,拖进来一个label控件来接受BS的参数,并显示,右击生成,复制该文件的bin目录下的exe,例如放在以下路径,例如C:\\simu\\下, 2.编 ...

  6. C#打开指定路径文件对话框

    private string OpenFileDlog(string DeafultDir) { OpenFileDialog Ofd = new OpenFileDialog(); Ofd.AddE ...

  7. 数据库SQL语句练习题10--18

    10.查询Score表中的最高分的学生学号和课程号.(子查询或者排序) select t.sno,t.cno from SCORE t where degree = (select max(degre ...

  8. Android-activity-intent

    package com.hanqi.myapplication; import android.content.ComponentName; import android.content.Intent ...

  9. An Example Of Validating Text Item In Oracle Forms Using When-Validate-Item Trigger

    Example is given below to validate a Text Item in Oracle Forms with specific rules condition which c ...

  10. 格式化时间(SimpleDateFormat)

    import java.text.SimpleDateFormat; import java.util.Date; public class Main{ public static void main ...