Java批量寫入文件和下載圖片的示例代碼
很久沒有在WhitMe上寫日記了,因?yàn)橛X著在App上寫私密日記的話肯定是不安全的,但是想把日記存下來。,然后看到有導(dǎo)出日記的功能,就把日記導(dǎo)出了(還好可以直接導(dǎo)出,不然就麻煩點(diǎn))。導(dǎo)出的是一個(gè)html文件??梢灾苯哟蜷_,排版都還在。
看了下源碼,是把日記存在一個(gè)json數(shù)組里了,圖片還是在服務(wù)器,利用url訪問,文字是在本地了。 但是想把圖片下載到本地,然后和文字對(duì)應(yīng),哪篇日記下的哪些圖片。
大概是如下的json數(shù)組。 大概有幾百條,分別是頭像、內(nèi)容:文字||內(nèi)容:圖片、時(shí)間。 簡單明了的json結(jié)構(gòu),就想著用java遍歷保存到本地。
[{ "avatar": "http://static.withme.cn/585****", "blocks": [{ "content": "今天天氣不錯(cuò)******", "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數(shù)組格式化確保正確然后轉(zhuǎn)成json數(shù)組遍歷。獲取到的圖片下載,文字寫入文檔。
public static void main(String[] args) { CloseableHttpClient client = null; JSONArray jsonArray = JSONArray.parseArray( "[{ "avatar": "http://static.withme.cn/585****", "blocks": [{ "content": "今天天氣不錯(cuò)******", "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 < array.size(); j++) { JSONObject jsPas = array.getJSONObject(j); // 遍歷 jsonarray 數(shù)組,把每一個(gè)對(duì)象轉(zhuǎn)成 json 對(duì)象 if (jsPas.get("type").equals("text")) { FileWriter fileWriter = null; try { String filePath = "f:/13/" + time; File dir = new File(filePath); // 檢查放置文件的文件夾路徑是否存在,不存在則創(chuàng)建 if (!dir.exists()) { dir.mkdirs();// mkdirs創(chuàng)建多級(jí)目錄 } File checkFile = new File(filePath + "/text" + time + "-" + j + ".txt"); // 檢查目標(biāo)文件是否存在,不存在則創(chuàng)建 if (!checkFile.exists()) { checkFile.createNewFile();// 創(chuàng)建目標(biāo)文件 } // FileWriter(File file, boolean append),append為true時(shí)為追加模式,false或缺省則為覆蓋模式 fileWriter = new FileWriter(checkFile, true); String url = jsPas.get("content").toString(); // 向目標(biāo)文件中寫入內(nèi)容 fileWriter.append(url); fileWriter.flush(); System.out.println("寫入成功??!"); } catch (IOException e) { e.printStackTrace(); } finally { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } if (jsPas.get("type").equals("pic")) { client = HttpClients.createDefault(); String url = jsPas.get("content").toString(); String path = "f:/13/" + time; // System.out.println(jsPas.get("content")); httpGetImg(client, url, path + "/pic" + time + "-" + j + ".jpg"); System.out.println("ok"); } } } } catch (Exception e) { e.printStackTrace(); } finally { if (client != null) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 發(fā)送get請(qǐng)求, 下載圖片 * * @param url 路徑 * @return */ public static void httpGetImg(CloseableHttpClient client, String imgUrl, String savePath) { // 發(fā)送get請(qǐng)求 HttpGet request = new HttpGet(imgUrl); // 設(shè)置請(qǐng)求和傳輸超時(shí)時(shí)間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build(); // 設(shè)置請(qǐng)求頭 request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1"); request.setConfig(requestConfig); try { CloseableHttpResponse response = client.execute(request); if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) { HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); FileUtils.copyInputStreamToFile(in, new File(savePath)); System.out.println("下載圖片成功:" + imgUrl); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { request.releaseConnection(); } }
JAr包:
<!-- apache io操作通用jar包 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <!-- httpclient 支持jar --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.5</version> </dependency>
運(yùn)行結(jié)果:
保存到本地:
以上就是Java批量寫入文件和下載圖片的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java批量寫入和下載的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring的@PreAuthorize注解自定義權(quán)限校驗(yàn)詳解
這篇文章主要介紹了Spring的@PreAuthorize注解自定義權(quán)限校驗(yàn)詳解,由于項(xiàng)目中,需要對(duì)外開放接口,要求做請(qǐng)求頭校驗(yàn),不做其他權(quán)限控制,所以準(zhǔn)備對(duì)開放的接口全部放行,不做登錄校驗(yàn),需要的朋友可以參考下2023-11-11詳解CopyOnWriteArrayList是如何保證線程安全
這篇文章主要為大家介紹了CopyOnWriteArrayList是如何保證線程安全講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Springboot+mybatis plus找不到mapper.xml的問題解決
本文主要介紹了Springboot+mybatis plus找不到mapper.xml的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Java中valueOf和parseInt的區(qū)別詳解
這篇文章主要介紹了Java中valueOf和parseInt的區(qū)別詳解,在編程中,遇到類型轉(zhuǎn)換,好像會(huì)經(jīng)常用到 parseInt 和 valueOf,當(dāng)然這里只拿 Integer 類型進(jìn)行陳述,其他類型也是雷同的,需要的朋友可以參考下2024-01-01Java爬蟲范例之使用Htmlunit爬取學(xué)校教務(wù)網(wǎng)課程表信息
htmlunit 是一款開源的java 頁面分析工具,讀取頁面后,可以有效的使用htmlunit分析頁面上的內(nèi)容。項(xiàng)目可以模擬瀏覽器運(yùn)行,被譽(yù)為java瀏覽器的開源實(shí)現(xiàn)。今天我們用這款分析工具來爬取學(xué)校教務(wù)網(wǎng)課程表信息2021-11-11JavaCV實(shí)現(xiàn)讀取視頻信息及自動(dòng)截取封面圖詳解
javacv可以幫助我們?cè)趈ava中很方便的使用OpenCV以及FFmpeg相關(guān)的功能接口。本文將利用Javacv實(shí)現(xiàn)在視頻網(wǎng)站中常見的讀取視頻信息和自動(dòng)獲取封面圖的功能,感興趣的可以了解一下2022-06-06數(shù)據(jù)庫連接池c3p0配置_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了數(shù)據(jù)庫連接池c3p0配置的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08