• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java批量写入文件和下载图片的示例代码

java 搞代码 4年前 (2022-01-05) 33次浏览 已收录 0个评论

这篇文章主要介绍了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搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java批量写入文件和下载图片的示例代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址