很久没有在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数组遍历。获取到的图片下载,文字写入文档。
public static void main(String[] args) { CloseableHttpClient client = null; JSONArray jsOnArray= 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 (int m = 0; m
JAr包:
commons-io commons-io 2.4 org.apache.httpcomponents httpclient 4.3.5 org.apache.httpcomponents httpmime 4.3.5
运行结果:
保存到本地:
以上就是Java批量写入文件和下载图片的示例代码的详细内容,更多关于Java批量写入和下载的资料请关注其它相关文章!