已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
基于Java实现批量下载网络图片昨天朋友做项目遇到一个需求,需要把上千个的微博表情图片下载到本地磁盘,并做好规范命名,塞给我一堆Json数据,让我帮忙处理下,反正闲着也没事干,就帮忙写了。(很简单的一个功能,随手记录下,刚好填补下最近博客的空白)由于只是方便自己的工具,就不需要什么图形界面了,就用Java去写了,先看下效果图嘿嘿,突然发现会写程序是件好事,一千多张表情图片要是手动下载再进行改名,非得忙个2天2夜不可。好了,言归正传,说下代码实现,分成3步:1、获取Json数据2、根据Json数据所提供的图片资源地址进行下载3、分类,规范命名先来看下Json数据格式:为了方便操作,我封装了一个数据实体类1. packagecom.lcw.downloadutil.domain;2. 3. publicclassBean4. 5. privateStringphrase;6. privateStringtype;7. privateStringurl;8. privateBooleanhot;9. privateBooleancommon;10. privateStringcategory;11. privateStringicon;12. privateStringvalue;13. privateStringpicid;14. 15. publicStringgetPhrase()16. returnphrase;17. 18. 19. publicvoidsetPhrase(Stringphrase)20. this.phrase=phrase;21. 22. 23. publicStringgetType()24. returntype;25. 26. 27. publicvoidsetType(Stringtype)28. this.type=type;29. 30. 31. publicStringgetUrl()32. returnurl;33. 34. 35. publicvoidsetUrl(Stringurl)36. this.url=url;37. 38. 39. publicBooleangetHot()40. returnhot;41. 42. 43. publicvoidsetHot(Booleanhot)44. this.hot=hot;45. 46. 47. publicBooleangetCommon()48. returncommon;49. 50. 51. publicvoidsetCommon(Booleancommon)52. mon=common;53. 54. 55. publicStringgetCategory()56. returncategory;57. 58. 59. publicvoidsetCategory(Stringcategory)60. this.category=category;61. 62. 63. publicStringgetIcon()64. returnicon;65. 66. 67. publicvoidsetIcon(Stringicon)68. this.icon=icon;69. 70. 71. publicStringgetValue()72. returnvalue;73. 74. 75. publicvoidsetValue(Stringvalue)76. this.value=value;77. 78. 79. publicStringgetPicid()80. returnpicid;81. 82. 83. publicvoidsetPicid(Stringpicid)84. this.picid=picid;85. 86. 87. Override88. publicStringtoString()89. returnBeanphrase=+phrase+,type=+type+,url=+url+,hot=+hot+,common=+common+,category=+category+,icon=+icon+,value=+value+,picid=+picid+;90. 91. 92. 然后我写了一个工具类封装了一些方法分别用来处理(网络数据的获取,Json数据的反序列化,对图片资源的下载)1. packagecom.lcw.downloadutil.utils;2. 3. importjava.io.BufferedInputStream;4. importjava.io.BufferedOutputStream;5. importjava.io.BufferedReader;6. importjava.io.File;7. importjava.io.FileOutputStream;8. importjava.io.IOException;9. importjava.io.InputStream;10. importjava.io.InputStreamReader;11. .MalformedURLException;12. .URL;13. importjava.util.List;14. 15. importcom.google.gson.Gson;16. importcom.google.gson.reflect.TypeToken;17. importcom.lcw.downloadutil.domain.Bean;18. 19. /*20. *工具类集合21. *22. *authorRabbit_Lee23. *24. */25. publicclassHelpUtils26. /*27. *根据所提供的url地址获取Json数据28. *29. *parampath30. *return31. */32. publicStringgetHttpString(Stringpath)33. /存放获取到的数据34. Stringinfo=;35. /网络请求所需变量36. InputStreamin=null;37. InputStreamReaderreader=null;38. BufferedReaderbufferedReader=null;39. try40. URLurl=newURL(path);41. /根据Url打开地址,以utf-8编码的形式返回输入流42. in=url.openStream();43. reader=newInputStreamReader(in,utf-8);44. bufferedReader=newBufferedReader(reader);45. /临时接受数据变量46. Stringtemp=null;47. while(temp=bufferedReader.readLine()!=null)48. info+=temp;49. 50. returninfo;51. catch(MalformedURLExceptione)52. e.printStackTrace();53. catch(IOExceptione)54. e.printStackTrace();55. finally56. try57. in.close();58. reader.close();59. bufferedReader.close();60. catch(IOExceptione)61. e.printStackTrace();62. 63. 64. returnnull;65. 66. 67. /*68. *将所提供的Json数据反序列化成Java对象(List集合)69. *70. *paramjson71. *return72. */73. publicListchangeJsonToList(Stringjson)74. /利用Gson将JSON数据反序列化成JAVA对象75. Gsongson=newGson();76. Listbeans=gson.fromJson(json,newTypeTokenList()77. .getType();78. returnbeans;79. 80. 81. /*82. *下载图片,并按照指定的路径存储83. *parambean84. *paramfilePath85. */86. publicvoidmakeImage(Beanbean,StringfilePath)87. /网络请求所需变量88. try89. /获取输入流90. BufferedInputStreamin=newBufferedInputStream(newURL(bean.getUrl().openStream();91. /创建文件流92. Filefile=newFile(filePath+bean.getPhrase()+.gif);93. BufferedOutputStreamout=newBufferedOutputStream(newFileOutputStream(file);94. /缓冲字节数组95. bytedata=newbyte2048;96. intlength=in.read(data);97. while(length!=-1)98. out.write(data,0,data.length);99. length=in.read(data);100. 101. System.out.println(正在执行下载任务:当前正在下载图片+bean.getPhrase()+.gif);102. in.close();103. out.close();104. catch(MalformedURLExceptione)105. e.printStackTrace();106. catch(IOExceptione)107. e.printStackTrace();108. 109. 110. 111. 上面代码对于Json数据的处理,我用到了谷歌给我们提供的Gson工具类对于Gson类不懂使用的朋友可以看下我之前写过的一篇文章:Gson简要使用笔记:/lichenwei/p/3987429.html1. packagecom.lcw.downloadutil.main;2. 3. importjava.util.List;4. 5. importcom.lcw.downloadutil.domain.Bean;6. importcom.lcw.downloadutil.utils.HelpUtils;7. 8. publicclassTaskMain9. 10. privatestaticfinalStringURL=这里涉及到Oauth2.0的一些个人隐私数据就不给出了;11. privatestaticStringmJsonInfo;12. 13. publicstaticvoidmain(Stringargs)14. HelpUtilshelpUtils=newHelpUtils();15. /获取Json数据16. mJsonInfo=helpUtils.getHttpString(URL);17. /将Json数据反序列化成java对象18. Listbeans=helpUtils.changeJsonToList(mJsonInfo);19. /循环遍历下载图片20. for(inti=0;ibeans.size();i+)21. helpUtils.makeImage(beans.get(i),C:/images/);22. 23. 24. 25. 26. 到这里就完事了,有哪里不清楚的朋友,可以在下面文章评论交流。作者:Balla_兔子出处:/lichenwei/本文版权归作者和博客园共有,欢迎转载,但未经
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 省护理管理干部培训心得
- 八上 20苏州园林 课件(共23张)
- 2023年税法大作业答案
- 2023年药物分析题库
- 2021年部编人教版四年级语文上册期中试卷(附答案)
- 12.2 一次函数(第6课时 一次函数与一元一次方程、不等式)课件-2026-2027学年八年级数学上册沪科版
- 2026年桩基成桩验收施工组织设计-综合办公楼
- 2026年四年级科学简单电路与导体期中模拟卷
- 2026-2027学年统编版八上道法-第12.1课《认识国家安全》
- 中国经典寓言故事
- 2026中国舞蹈艺术行业市场考察及产业发展与政策影响分析报告
- 药物合成反应第五章重排反应
- 2026茯苓种植经济效益与可持续发展策略咨询报告
- 口服抗栓药物消化道损伤防治共识2026
- (高级三级)互联网营销师(直播销售员)职业技能鉴定理论考试题库及答案
- 村级报账员队伍管理
- 生产经理入职工作方案
- 土墙草泥抹面施工方案
- 2025年金融工程行业现状分析报告及未来五至十年发展趋势
- 围炉煮茶活动策划
- 激光焊接培训知识总结
评论
0/150
提交评论