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(); } } |