这篇文章主要介绍了Java批量写入文件和下载图片的示例代码,帮助大家更好的理解和使用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.c<strong style="color:transparent">来源gao@daima#com搞(%代@#码@网</strong>n/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 <jsonArray.size(); m++) { JSONObject jsonPas = jsonArray.getJSONObject(m); JSONArray array = JSONArray.parseArray(jsonPas.get("blocks").toString()); String time = jsonPas.get("dateStr").toString(); for (int j = 0; j </div><p>JAr包:</p><div class="gaodaimacode"><pre class="prettyprint linenums"> <!-- apache io操作通用jar包 --> commons-iocommons-io2.4<!-- httpclient 支持jar --> org.apache.httpcomponentshttpclient4.3.5 org.apache.httpcomponentshttpmime4.3.5
运行结果:
保存到本地:
以上就是Java批量写入文件和下载图片的示例代码的详细内容,更多关于Java批量写入和下载的资料请关注gaodaima搞代码网其它相关文章!
以上就是Java批量写入文件和下载图片的示例代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!