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();
}
}
}
}
}
java多线程下载网络图片的更多相关文章
- JAVA多线程下载网络文件
JAVA多线程下载网络文件,开启多个线程,同时下载网络文件. 源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...
- java多线程下载和断点续传
java多线程下载和断点续传,示例代码只实现了多线程,断点只做了介绍.但是实际测试结果不是很理想,不知道是哪里出了问题.所以贴上来请高手修正. [Java]代码 import java.io.File ...
- java 多线程下载功能
import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; impo ...
- java 多线程下载文件 以及URLConnection和HttpURLConnection的区别
使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...
- Java多线程下载文件
package com.test.download; import java.io.File; import java.io.InputStream; import java.io.RandomA ...
- Java多线程下载器FileDownloader(支持断点续传、代理等功能)
前言 在我的任务清单中,很早就有了一个文件下载器,但一直忙着没空去写.最近刚好放假,便抽了些时间完成了下文中的这个下载器. 介绍 同样的,还是先上效果图吧. Jar包地址位于 FileDownload ...
- Java多线程下载初试
一.服务端/客户端代码的实现 服务端配置config @ConfigurationProperties("storage") public class StoragePropert ...
- Java多线程下载分析
为什么要多线程下载 俗话说要以终为始,那么我们首先要明确多线程下载的目标是什么,不外乎是为了更快的下载文件.那么问题来了,多线程下载文件相比于单线程是不是更快? 对于这个问题可以看下图. 横坐标是线程 ...
- java 多线程下载文件并实时计算下载百分比(断点续传)
多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...
随机推荐
- C堆栈
C堆栈实现的表达式求值 //Luangeng #include<stdio.h> #include<conio.h> #include<windows.h> #de ...
- c++学习总结--复合类型,const。
复习c++,有必要对一些东西做一下笔记,方便以后学习,如有问题,欢迎提出. 首先,最常用两种复合类型应该是引用和指针. 所谓复合类型,是在基本类型基础(如:int)上定义的类型. 引用是c++的一个复 ...
- 关于C指针
地址概念:内存基本单元是一个字节,一个字节8个位.在定义变量的时候,如int a=10:系统为变量a分配2个字节空间:1000~1001,并在1001~1002中存有数据10.内存中没有变量a,只有1 ...
- JavaBean基础转载
JavaWeb:JavaBean基础 JavaBean基础 JavaBean简介: 1.JavaBean是一种可以重复使用的类,可以没有用户界面,主要负责业务数据或者处理事物(数据运算.操作数据库) ...
- [转]NSTimer和CADisplayLink的基本用法
简要区别:NSTimer初始化器接受调用方法逻辑之间的间隔作为它的其中一个参数,预设一秒执行30次.CADisplayLink默认每秒运行60次,通过它的frameInterval属性改变每秒运行帧数 ...
- C#其他
1.switch - if ...else if...switch(表达式) { case 值: ..... break; case 值: ..... break; default: ..... br ...
- Android 杂记 - 存货盘点用的客户端
最近有个盘点用的东西,要放到移动设备,本来用 .Net Compact Framework,CAB 部署在 CE 系统的移动条码设备.技术太旧,我用了这个周末两天时间,把这东西在试试实现在安卓上面,给 ...
- Berkeley DB的常见API简单分析
1.用来存储类信息的数据库不要求能够存储重复的关键字 例: dbConfig.setSortedDuplicates(false);2.DatabaseEntrt能够支持任何能够转化为by ...
- Shared Assembilies and Strongly Named Assemblies
the .NET Framework has in place to deal with versioning problems. Two Kinds of Assemblies, Two Kinds ...
- 使用httpclient时候,出现“Too many open files”问题
最近接触的sendHttpPostRequest的问题比较多,近期碰到了关于 "java.net.SocketException: Too many open files" 的问题 ...