From 18ebe0c9f93712a44a543c50ea5722668e4c36d7 Mon Sep 17 00:00:00 2001 From: changpengfei Date: Wed, 8 Sep 2021 11:13:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=AF=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../talkonlineweb/controller/UserContoller.java | 1 - .../com/lyms/talkonlineweb/util/HXService.java | 115 ++++++++++++++++++++- 2 files changed, 112 insertions(+), 4 deletions(-) diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java index b20e6f3..2dbd582 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java @@ -72,7 +72,6 @@ public class UserContoller { user.setHxid(rArr.getJSONObject(0).getString("uuid")); } } - JSONObject json=hxService.getChatMessages("2021090617"); lymsUserService.saveOrUpdate(user); user.setPasswd(null); tMap.put("user",user); diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java index a1d1d45..bd6f406 100644 --- a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java @@ -1,8 +1,11 @@ package com.lyms.talkonlineweb.util; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.util.IOUtils; import lombok.extern.log4j.Log4j2; +import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -11,12 +14,18 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import org.springframework.util.FileSystemUtils; import org.springframework.web.client.RestTemplate; +import java.io.*; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.zip.GZIPInputStream; + +import static org.apache.commons.io.FilenameUtils.getExtension; /** * 环信相关API @@ -156,18 +165,109 @@ public class HXService { * @param time 查询的时间格式为10位数字形式(YYYYMMDDHH) * @return */ - public JSONObject getChatMessages(String time){ + public List getChatMessages(String time){ + List cLst=new ArrayList<>(); JSONObject msg=new JSONObject(); String token=getToken(); HttpHeaders headers=new HttpHeaders(); headers.add("Authorization","Bearer "+token); HttpEntity param=new HttpEntity(headers); - resp=restTemplate.exchange(getUrl()+"chatmessages/"+time, HttpMethod.GET,param,String.class); + try { + resp = restTemplate.exchange(getUrl() + "chatmessages/" + time, HttpMethod.GET, param, String.class); + if (resp.getStatusCodeValue()==200){ msg= JSON.parseObject(resp.getBody()); log.info(msg); + JSONArray data=msg.getJSONArray("data"); + if(data.size()>0){ + for (int i = 0; i < data.size() ; i++) { + String url=data.getJSONObject(i).getString("url"); + File file=getFileFromUrl(url); + FileUtils.copyURLToFile(new URL(url),file); + file=doUncompressFile(file); + cLst=FileUtils.readLines(file,"utf-8"); + } + } + } + }catch (Exception e){ + log.error(e.getMessage()); + e.printStackTrace(); + } + return cLst; + } + + + private File getFileFromUrl(String url){ + + String preStr=url.split("\\?")[0]; + String[] preArr=preStr.split("/"); + File file=new File(preArr[preArr.length-1]); + if(file.exists()){ + file.delete(); } - return msg; + return file; + } + + /** + * Uncompress the incoming file. 解压缩的文件。 + * @param inFileName 未压缩文件的名称 + */ + private static File doUncompressFile(File inFileName) { + + try { + + if (!getExtension(inFileName.getName()).equalsIgnoreCase("gz")) { + System.err.println("文件名必须有扩展名 \".gz\""); + System.exit(1); + } + + System.out.println("打开压缩文件."); + GZIPInputStream in = null; + try { + in = new GZIPInputStream(new FileInputStream(inFileName)); + } catch(Exception e) { + System.err.println("文件未找到. " + inFileName); + System.exit(1); + } + + System.out.println("打开输出文件."); + String outFileName = getFileName(inFileName.getName()); + File file=new File(outFileName); + FileOutputStream out = null; + try { + out = new FileOutputStream(file,false); + } catch (FileNotFoundException e) { + System.err.println("不能写入文件. " + outFileName); + System.exit(1); + } + + System.out.println("将字节从压缩文件传输到输出文件."); + byte[] buf = new byte[1024]; + int len; + while((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + + System.out.println("关闭文件和流"); + in.close(); + out.close(); + return file; + } catch (IOException e) { + e.printStackTrace(); +// System.exit(1); + } + + return null; + } + + public static String getFileName(String f) { + String fname = ""; + int i = f.lastIndexOf('.'); + + if (i > 0 && i < f.length() - 1) { + fname = f.substring(0,i); + } + return fname; } /** @@ -200,4 +300,13 @@ public class HXService { return rs; } + + public static void main(String[] args) { + String url="http://ebs-chatmessage-a1.easemob.com/history/7D/1135210903239178/demo/2021090617.gz?Expires=1630978921&OSSAccessKeyId=LTAI4Fy3Zj6ha5XFZEMdRidM&Signature=DvTfmFwCqE9HcjE5eAZCrBfpJLg%3D"; + String preStr=url.split("\\?")[0]; + System.out.println(preStr); + String[] preArr=preStr.split("/"); + System.out.println(preArr[preArr.length-1]); +} + } -- 1.8.3.1