色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Java批量寫入文件和下載圖片的示例代碼

瀏覽:3日期:2022-08-24 14:10:45

很久沒有在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 < 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 數組,把每一個對象轉成 json 對象 if (jsPas.get('type').equals('text')) { FileWriter fileWriter = null; try { String filePath = 'f:/13/' + time; File dir = new File(filePath); // 檢查放置文件的文件夾路徑是否存在,不存在則創建 if (!dir.exists()) {dir.mkdirs();// mkdirs創建多級目錄 } File checkFile = new File(filePath + '/text' + time + '-' + j + '.txt'); // 檢查目標文件是否存在,不存在則創建 if (!checkFile.exists()) {checkFile.createNewFile();// 創建目標文件 } // FileWriter(File file, boolean append),append為true時為追加模式,false或缺省則為覆蓋模式 fileWriter = new FileWriter(checkFile, true); String url = jsPas.get('content').toString(); // 向目標文件中寫入內容 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();} } } } /** * 發送get請求, 下載圖片 * * @param url 路徑 * @return */ public static void httpGetImg(CloseableHttpClient client, String imgUrl, String savePath) { // 發送get請求 HttpGet request = new HttpGet(imgUrl); // 設置請求和傳輸超時時間 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(50000).build(); // 設置請求頭 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>

運行結果:

Java批量寫入文件和下載圖片的示例代碼

保存到本地:

Java批量寫入文件和下載圖片的示例代碼

以上就是Java批量寫入文件和下載圖片的示例代碼的詳細內容,更多關于Java批量寫入和下載的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 中文在线最新版天堂 | 欧美成人免费在线观看 | 国产欧美一区二区三区免费看 | 久久国产成人精品国产成人亚洲 | 一区二区三区不卡在线观看 | 大陆孕妇孕交视频自拍 | 国产精品亚洲天堂 | 免费男女视频 | 国产精品日本 | 免费人成黄页网站在线观看 | 日韩视频专区 | 成人伊人青草久久综合网 | 黄色欧美网站 | 久久精品网站免费观看调教 | 国产成人综合久久精品亚洲 | 亚欧美视频| 国内主播福利视频在线观看 | 韩国视频一区 | 亚洲人成一区二区三区 | 亚洲爽爽 | 日韩麻豆| 国产精品久久久久久久久福利 | 欧洲成人免费视频 | 中国一级毛片视频 | 最新国产午夜精品视频成人 | 丝袜黄色片| 国内精品久久久久影院老司 | 欧美视频网站在线观看 | 日本免费不卡在线一区二区三区 | 免费看成人频视在线视频 | 国产成人免费福利网站 | 目韩一区二区三区系列片丶 | 亚洲成人免费网址 | 中文字幕精品一区二区三区视频 | 亚洲经典乱码在线播 | 99re6这里有精品热视频在线 | 成人免费网站视频www | 亚洲成人在线播放视频 | 99精品免费久久久久久久久日本 | 亚洲欧美精品成人久久91 | 国产偷怕自拍 |