基于Java实现批量下载网络图片

昨天朋友做项目遇到一个需求,需要把上千个的微博表情图片下载到本地磁盘,并做好规范命名,塞给我一堆Json数据,让我帮忙处理下,反正闲着也没事干,就帮忙写了。(很简单的一个功能,随手记录下,刚好填补下最近博客的空白)

由于只是方便自己的工具,就不需要什么图形界面了,就用Java去写了,先看下效果图~

基于Java实现批量下载网络图片 基于Java实现批量下载网络图片

基于Java实现批量下载网络图片基于Java实现批量下载网络图片

嘿嘿,突然发现会写程序是件好事,一千多张表情图片要是手动下载再进行改名,非得忙个2天2夜不可。。

好了,言归正传,说下代码实现,分成3步:

1、获取Json数据

2、根据Json数据所提供的图片资源地址进行下载

3、分类,规范命名

先来看下Json数据格式:

基于Java实现批量下载网络图片

为了方便操作,我封装了一个数据实体类

 package com.lcw.downloadutil.domain;

 public class Bean {

     private String phrase;
private String type;
private String url;
private Boolean hot;
private Boolean common;
private String category;
private String icon;
private String value;
private String picid; public String getPhrase() {
return phrase;
} public void setPhrase(String phrase) {
this.phrase = phrase;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public Boolean getHot() {
return hot;
} public void setHot(Boolean hot) {
this.hot = hot;
} public Boolean getCommon() {
return common;
} public void setCommon(Boolean common) {
this.common = common;
} public String getCategory() {
return category;
} public void setCategory(String category) {
this.category = category;
} public String getIcon() {
return icon;
} public void setIcon(String icon) {
this.icon = icon;
} public String getValue() {
return value;
} public void setValue(String value) {
this.value = value;
} public String getPicid() {
return picid;
} public void setPicid(String picid) {
this.picid = picid;
} @Override
public String toString() {
return "Bean [phrase=" + phrase + ", type=" + type + ", url=" + url + ", hot=" + hot + ", common=" + common + ", category=" + category + ", icon=" + icon + ", value=" + value + ", picid=" + picid + "]";
} }

然后我写了一个工具类封装了一些方法

分别用来处理(网络数据的获取,Json数据的反序列化,对图片资源的下载)

 package com.lcw.downloadutil.utils;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
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.util.List; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lcw.downloadutil.domain.Bean; /**
* 工具类集合
*
* @author Rabbit_Lee
*
*/
public class HelpUtils {
/**
* 根据所提供的url地址获取Json数据
*
* @param path
* @return
*/
public String getHttpString(String path) {
// 存放获取到的数据
String info = "";
// 网络请求所需变量
InputStream in = null;
InputStreamReader reader = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(path);
// 根据Url打开地址,以utf-8编码的形式返回输入流
in = url.openStream();
reader = new InputStreamReader(in, "utf-8");
bufferedReader = new BufferedReader(reader);
// 临时接受数据变量
String temp = null;
while ((temp = bufferedReader.readLine()) != null) {
info += temp;
}
return info;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
reader.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
} /**
* 将所提供的Json数据反序列化成Java对象(List集合)
*
* @param json
* @return
*/
public List<Bean> changeJsonToList(String json) {
// 利用Gson将JSON数据反序列化成JAVA对象
Gson gson = new Gson();
List<Bean> beans = gson.fromJson(json, new TypeToken<List<Bean>>() {
}.getType());
return beans;
} /**
* 下载图片,并按照指定的路径存储
* @param bean
* @param filePath
*/
public void makeImage(Bean bean, String filePath) {
// 网络请求所需变量
try {
//获取输入流
BufferedInputStream in = new BufferedInputStream(new URL(bean.getUrl()).openStream());
//创建文件流
File file = new File(filePath + bean.getPhrase()+".gif");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
//缓冲字节数组
byte[] data = new byte[2048];
int length = in.read(data);
while (length != -1) {
out.write(data, 0, data.length);
length = in.read(data);
}
System.out.println("正在执行下载任务:当前正在下载图片" + bean.getPhrase() + ".gif");
in.close();
out.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }

上面代码对于Json数据的处理,我用到了谷歌给我们提供的Gson工具类

对于Gson类不懂使用的朋友可以看下我之前写过的一篇文章:

Gson简要使用笔记》:http://www.cnblogs.com/lichenwei/p/3987429.html

接着,就是调用主类:

 package com.lcw.downloadutil.main;

 import java.util.List;

 import com.lcw.downloadutil.domain.Bean;
import com.lcw.downloadutil.utils.HelpUtils; public class TaskMain { private static final String URL = "这里涉及到Oauth2.0的一些个人隐私数据就不给出了";
private static String mJsonInfo; public static void main(String[] args) {
HelpUtils helpUtils = new HelpUtils();
// 获取Json数据
mJsonInfo = helpUtils.getHttpString(URL);
// 将Json数据反序列化成java对象
List<Bean> beans = helpUtils.changeJsonToList(mJsonInfo);
//循环遍历下载图片
for (int i = 0; i < beans.size(); i++) {
helpUtils.makeImage(beans.get(i), "C:/images/");
} } }

到这里就完事了,有哪里不清楚的朋友,可以在下面文章评论交流。

作者:Balla_兔子
出处:http://www.cnblogs.com/lichenwei/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我!

上一篇:UWP入门(十一)--使用选取器打开文件和文件夹


下一篇:redis过期回调以及键空间通知