Java批量写入文件和下载图片的示例代码
很久没有在WhitMe上写日记了,因为觉着在App上写私密日记的话肯定是不安全的,但是想把日记存下来。,然后看到有导出日记的功能,就把日记导出了(还好可以直接导出,不然就麻烦点)。导出的是一个html文件。可以直接打开,排版都还在。
看了下源码,是把日记存在一个json数组里了,图片还是在服务器,利用url访问,文字是在本地了。但是想把图片下载到本地,然后和文字对应,哪篇日记下的哪些图片。
大概是如下的json数组。大概有几百条,分别是头像、内容:文字||内容:图片、时间。简单明了的json结构,就想着用java遍历保存到本地。
[{ "avatar":"http://static.withme.cn/585****", "blocks":[{ "content":"今天天气不错******", "type":"text" },{ "content":"http://static.withme.cn/84ac***", "type":"pic" },{ "content":"http://static.withme.cn/5af2c***", "type":"pic" },{ "content":"http://static.withme.cn/9a4e****", "type":"pic" },{ "content":"http://static.withme.cn/9ffdb***", "type":"pic" },{ "content":"http://static.withme.cn/da5e7db***", "type":"pic" },{ "content":"http://static.withme.cn/e6ccf3764***", "type":"pic" },{ "content":"http://static.withme.cn/73ca***", "type":"pic" },{ "content":"http://static.wi***", "type":"pic" },{ "content":"http://static.withme.cn/4cf7dde****", "type":"pic" }], "dateStr":"2018-09-03", "timeStr":"18:59:41" },{...},...]
将json数组格式化确保正确然后转成json数组遍历。获取到的图片下载,文字写入文档。
publicstaticvoidmain(String[]args){ CloseableHttpClientclient=null; JSONArrayjsonArray=JSONArray.parseArray( "[{ "avatar":"http://static.withme.cn/585****", "blocks":[{ "content":"今天天气不错******", "type":"text" },{ "content":"http://static.withme.cn/84ac***", "type":"pic" },{ "content":"http://static.withme.cn/5af2c***", "type":"pic" },{ "content":"http://static.withme.cn/9a4e****", "type":"pic" },{ "content":"http://static.withme.cn/9ffdb***", "type":"pic" },{ "content":"http://static.withme.cn/da5e7db***", "type":"pic" },{ "content":"http://static.withme.cn/e6ccf3764***", "type":"pic" },{ "content":"http://static.withme.cn/73ca***", "type":"pic" },{ "content":"http://static.wi***", "type":"pic" },{ "content":"http://static.withme.cn/4cf7dde****", "type":"pic" }], "dateStr":"2018-09-03", "timeStr":"18:59:41" },{...},{...},...]"); try{ for(intm=0;mJAr包:
commons-io commons-io 2.4 org.apache.httpcomponents httpclient 4.3.5 org.apache.httpcomponents httpmime 4.3.5 运行结果:
保存到本地:
以上就是Java批量写入文件和下载图片的示例代码的详细内容,更多关于Java批量写入和下载的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。