Commit 18ebe0c9f93712a44a543c50ea5722668e4c36d7
1 parent
31a5965849
Exists in
master
and in
1 other branch
环信
Showing 2 changed files with 112 additions and 4 deletions
talkonlineweb/src/main/java/com/lyms/talkonlineweb/controller/UserContoller.java
View file @
18ebe0c
talkonlineweb/src/main/java/com/lyms/talkonlineweb/util/HXService.java
View file @
18ebe0c
1 | 1 | package com.lyms.talkonlineweb.util; |
2 | 2 | |
3 | 3 | import com.alibaba.fastjson.JSON; |
4 | +import com.alibaba.fastjson.JSONArray; | |
4 | 5 | import com.alibaba.fastjson.JSONObject; |
6 | +import com.alibaba.fastjson.util.IOUtils; | |
5 | 7 | import lombok.extern.log4j.Log4j2; |
8 | +import org.apache.commons.io.FileUtils; | |
6 | 9 | import org.springframework.beans.factory.annotation.Autowired; |
7 | 10 | import org.springframework.beans.factory.annotation.Value; |
8 | 11 | import org.springframework.context.annotation.Bean; |
9 | 12 | |
10 | 13 | |
11 | 14 | |
... | ... | @@ -11,13 +14,19 @@ |
11 | 14 | import org.springframework.http.HttpMethod; |
12 | 15 | import org.springframework.http.ResponseEntity; |
13 | 16 | import org.springframework.stereotype.Component; |
17 | +import org.springframework.util.FileSystemUtils; | |
14 | 18 | import org.springframework.web.client.RestTemplate; |
15 | 19 | |
20 | +import java.io.*; | |
21 | +import java.net.URL; | |
16 | 22 | import java.util.ArrayList; |
17 | 23 | import java.util.HashMap; |
18 | 24 | import java.util.List; |
19 | 25 | import java.util.Map; |
26 | +import java.util.zip.GZIPInputStream; | |
20 | 27 | |
28 | +import static org.apache.commons.io.FilenameUtils.getExtension; | |
29 | + | |
21 | 30 | /** |
22 | 31 | * 环信相关API |
23 | 32 | */ |
24 | 33 | |
25 | 34 | |
26 | 35 | |
27 | 36 | |
28 | 37 | |
... | ... | @@ -156,21 +165,112 @@ |
156 | 165 | * @param time 查询的时间格式为10位数字形式(YYYYMMDDHH) |
157 | 166 | * @return |
158 | 167 | */ |
159 | - public JSONObject getChatMessages(String time){ | |
168 | + public List<String> getChatMessages(String time){ | |
169 | + List<String> cLst=new ArrayList<>(); | |
160 | 170 | JSONObject msg=new JSONObject(); |
161 | 171 | String token=getToken(); |
162 | 172 | HttpHeaders headers=new HttpHeaders(); |
163 | 173 | headers.add("Authorization","Bearer "+token); |
164 | 174 | HttpEntity param=new HttpEntity(headers); |
165 | - resp=restTemplate.exchange(getUrl()+"chatmessages/"+time, HttpMethod.GET,param,String.class); | |
175 | + try { | |
176 | + resp = restTemplate.exchange(getUrl() + "chatmessages/" + time, HttpMethod.GET, param, String.class); | |
177 | + | |
166 | 178 | if (resp.getStatusCodeValue()==200){ |
167 | 179 | msg= JSON.parseObject(resp.getBody()); |
168 | 180 | log.info(msg); |
181 | + JSONArray data=msg.getJSONArray("data"); | |
182 | + if(data.size()>0){ | |
183 | + for (int i = 0; i < data.size() ; i++) { | |
184 | + String url=data.getJSONObject(i).getString("url"); | |
185 | + File file=getFileFromUrl(url); | |
186 | + FileUtils.copyURLToFile(new URL(url),file); | |
187 | + file=doUncompressFile(file); | |
188 | + cLst=FileUtils.readLines(file,"utf-8"); | |
189 | + } | |
190 | + } | |
169 | 191 | } |
170 | - return msg; | |
192 | + }catch (Exception e){ | |
193 | + log.error(e.getMessage()); | |
194 | + e.printStackTrace(); | |
195 | + } | |
196 | + return cLst; | |
171 | 197 | } |
172 | 198 | |
199 | + | |
200 | + private File getFileFromUrl(String url){ | |
201 | + | |
202 | + String preStr=url.split("\\?")[0]; | |
203 | + String[] preArr=preStr.split("/"); | |
204 | + File file=new File(preArr[preArr.length-1]); | |
205 | + if(file.exists()){ | |
206 | + file.delete(); | |
207 | + } | |
208 | + return file; | |
209 | + } | |
210 | + | |
173 | 211 | /** |
212 | + * Uncompress the incoming file. 解压缩的文件。 | |
213 | + * @param inFileName 未压缩文件的名称 | |
214 | + */ | |
215 | + private static File doUncompressFile(File inFileName) { | |
216 | + | |
217 | + try { | |
218 | + | |
219 | + if (!getExtension(inFileName.getName()).equalsIgnoreCase("gz")) { | |
220 | + System.err.println("文件名必须有扩展名 \".gz\""); | |
221 | + System.exit(1); | |
222 | + } | |
223 | + | |
224 | + System.out.println("打开压缩文件."); | |
225 | + GZIPInputStream in = null; | |
226 | + try { | |
227 | + in = new GZIPInputStream(new FileInputStream(inFileName)); | |
228 | + } catch(Exception e) { | |
229 | + System.err.println("文件未找到. " + inFileName); | |
230 | + System.exit(1); | |
231 | + } | |
232 | + | |
233 | + System.out.println("打开输出文件."); | |
234 | + String outFileName = getFileName(inFileName.getName()); | |
235 | + File file=new File(outFileName); | |
236 | + FileOutputStream out = null; | |
237 | + try { | |
238 | + out = new FileOutputStream(file,false); | |
239 | + } catch (FileNotFoundException e) { | |
240 | + System.err.println("不能写入文件. " + outFileName); | |
241 | + System.exit(1); | |
242 | + } | |
243 | + | |
244 | + System.out.println("将字节从压缩文件传输到输出文件."); | |
245 | + byte[] buf = new byte[1024]; | |
246 | + int len; | |
247 | + while((len = in.read(buf)) > 0) { | |
248 | + out.write(buf, 0, len); | |
249 | + } | |
250 | + | |
251 | + System.out.println("关闭文件和流"); | |
252 | + in.close(); | |
253 | + out.close(); | |
254 | + return file; | |
255 | + } catch (IOException e) { | |
256 | + e.printStackTrace(); | |
257 | +// System.exit(1); | |
258 | + } | |
259 | + | |
260 | + return null; | |
261 | + } | |
262 | + | |
263 | + public static String getFileName(String f) { | |
264 | + String fname = ""; | |
265 | + int i = f.lastIndexOf('.'); | |
266 | + | |
267 | + if (i > 0 && i < f.length() - 1) { | |
268 | + fname = f.substring(0,i); | |
269 | + } | |
270 | + return fname; | |
271 | + } | |
272 | + | |
273 | + /** | |
174 | 274 | * 创建聊天室 |
175 | 275 | * @param name |
176 | 276 | * @param description |
... | ... | @@ -199,6 +299,15 @@ |
199 | 299 | log.info(rs); |
200 | 300 | return rs; |
201 | 301 | } |
302 | + | |
303 | + | |
304 | + public static void main(String[] args) { | |
305 | + String url="http://ebs-chatmessage-a1.easemob.com/history/7D/1135210903239178/demo/2021090617.gz?Expires=1630978921&OSSAccessKeyId=LTAI4Fy3Zj6ha5XFZEMdRidM&Signature=DvTfmFwCqE9HcjE5eAZCrBfpJLg%3D"; | |
306 | + String preStr=url.split("\\?")[0]; | |
307 | + System.out.println(preStr); | |
308 | + String[] preArr=preStr.split("/"); | |
309 | + System.out.println(preArr[preArr.length-1]); | |
310 | +} | |
202 | 311 | |
203 | 312 | } |