昨天朋友做项目遇到一个需求,需要把上千个的微博表情图片下载到本地磁盘,并做好规范命名,塞给我一堆Json数据,让我帮忙处理下,反正闲着也没事干,就帮忙写了。(很简单的一个功能,随手记录下,刚好填补下最近博客的空白)由于只是方便自己的工具,就不需要什么图形界面了,就用Java去写了,先看下效果图~ 嘿嘿,突然发现会写程序是件好事,一千多张表情图片要是手动下载再进行改名,非得忙个2天2夜不可。。好了,言归正传,说下代码实现,分成3步:1、获取Json数据2、根据Json数据所提供的图片资源地址进行下载3、分类,规范命名 先来看下Json数据格式:为了方便操作,我封装了一个数据实体类1packagecom.lcw.downloadutil.domain;23publicclassBean {45privateString phrase;6privateString type;7privateString url;8privateBoolean hot;9privateBoolean common;10privateString category;11privateString icon;12privateString value;13privateString picid;1415publicString getPhrase() {16returnphrase;17}1819publicvoidsetPhrase(String phrase) {20this.phrase =phrase;21}2223publicString getType() {24returntype;25}2627publicvoidsetType(String type) {28this.type =type;29}3031publicString getUrl() {32returnurl;33}3435publicvoidsetUrl(String url) {36this.url =url;37}3839publicBoolean getHot() {40returnhot;41}4243publicvoidsetHot(Boolean hot) {44this.hot =hot;45}4647publicBoolean getCommon() {48returncommon;49}5051publicvoidsetCommon(Boolean common) {52this.common =common;53}5455publicString getCategory() {56returncategory;57}5859publicvoidsetCategory(String category) {60this.category =category;61}6263publicString getIcon() {64returnicon;65}6667publicvoidsetIcon(String icon) {68this.icon =icon;69}7071publicString getValue() {72returnvalue;73}7475publicvoidsetValue(String value) {76this.value =value;77}7879publicString getPicid() {80returnpicid;81}8283publicvoidsetPicid(String picid) {84this.picid =picid;85}8687@Override88publicString toString() {89return"Bean [phrase=" + phrase + ", type=" + type + ", url=" + url + ", hot=" + hot + ", common=" + common + ", category=" + category + ", icon=" + icon + ", value=" + value + ", picid=" + picid + "]";90}9192} 然后我写了一个工具类封装了一些方法分别用来处理(网络数据的获取,Json数据的反序列化,对图片资源的下载)1packagecom.lcw.downloadutil.utils;23importjava.io.BufferedInputStream;4importjava.io.BufferedOutputStream;5importjava.io.BufferedReader;6importjava.io.File;7importjava.io.FileOutputStream;8importjava.io.IOException;9importjava.io.InputStream;10importjava.io.InputStreamReader;11importjava.net.MalformedURLException;12importjava.net.URL;13importjava.util.List;1415importcom.google.gson.Gson;16importcom.google.gson.reflect.TypeToken;17importcom.lcw.downloadutil.domain.Bean;1819/**20* 工具类集合21*22*@authorRabbit_Lee23*24*/25publicclassHelpUtils {26/**27* 根据所提供的url地址获取Json数据28*29*@parampath30*@return31*/32publicString getHttpString(String path) {33//存放获取到的数据34String info = "";35//网络请求所需变量36InputStream in =null;37InputStreamReader reader =null;38BufferedReader bufferedReader =null;39try{40URL url =newURL(path);41//根据Url打开地址,以utf-8编码的形式返回输入流42in =url.openStream();43reader =newInputStreamReader(in, "utf-8");44bufferedReader =newBufferedReader(reader);45//临时接受数据变量46String temp =null;47while((temp = bufferedReader.readLine()) !=null) {48info +=temp;49}50returninfo;51}catch(MalformedURLException e) {52e.printStackTrace();53}catch(IOException e) {54e.printStackTrace();55}finally{56try{57in.close();58reader.close();59bufferedReader.close();60}catch(IOException e) {61e.printStackTrace();62}63}64returnnull;65}6667/**68* 将所提供的Json数据反序列化成Java对象(List集合)69*70*@paramjson71*@return72*/73publicListchangeJsonToList(String json) {74//利用Gson将JSON数据反序列化成JAVA对象75Gson gson =newGson();76List beans = gson.fromJson(json,newTypeToken
>() {77}.getType());78returnbeans;79}8081/**82* 下载图片,并按照指定的路径存储83*@parambean84*@paramfilePath85*/86publicvoidmakeImage(Bean bean, String filePath) {87//网络请求所需变量88try{89//获取输入流90BufferedInputStream in =newBufferedInputStream(newURL(bean.getUrl()).openStream());91//创建文件流92File file =newFile(filePath + bean.getPhrase()+".gif");93BufferedOutputStream out =newBufferedOutputStream(newFileOutputStream(file));94//缓冲字节数组95byte[] data =newbyte[2048];96intlength =in.read(data);97while(length != -1) {98out.write(data, 0, data.length);99length =in.read(data);100}101System.out.println("正在执行下载任务:当前正在下载图片" + bean.getPhrase() + ".gif");102in.close();103out.close();104}catch(MalformedURLException e) {105e.printStackTrace();106}catch(IOException e) {107e.printStackTrace();108}109}110111}上面代码对于Json数据的处理,我用到了谷歌给我们提供的Gson工具类对于Gson类不懂使用的朋友可以看下我之前写过的一篇文章:《Gson简要使用笔记》:http://www.cnblogs.com/lichenwei/p/3987429.html 接着,就是调用主类:1packagecom.lcw.downloadutil.main;23importjava.util.List;45importcom.lcw.downloadutil.domain.Bean;6importcom.lcw.downloadutil.utils.HelpUtils;78publicclassTaskMain {910privatestaticfinalString URL = "这里涉及到Oauth2.0的一些个人隐私数据就不给出了";11privatestaticString mJsonInfo;1213publicstaticvoidmain(String[] args) {14HelpUtils helpUtils =newHelpUtils();15//获取Json数据16mJsonInfo =helpUtils.getHttpString(URL);17//将Json数据反序列化成java对象18List beans =helpUtils.changeJsonToList(mJsonInfo);19//循环遍历下载图片20for(inti = 0; i < beans.size(); i++) {21helpUtils.makeImage(beans.get(i), "C:/images/");22}2324}2526} 到这里就完事了,有哪里不清楚的朋友,可以在下面文章评论交流。作者:Balla_兔子出处:http://www.cnblogs.com/lichenwei/本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我! 本文链接:基于Java实现批量下载网络图片,转载请注明。