今天对接环信时,拉取聊天记录,发现是一个压缩包,不得不解压在存储
public static List<String> getChatHistory(String startTime) {
List<String> messageList = new ArrayList<>();
String fileUrl = "www.baid.com";
//请求失败或者暂时没有聊天记录
if (StringUtils.isBlank(fileUrl)) {
return messageList;
}
URLConnection conn = null;
//下载压缩包
try {
URL url = new URL(fileUrl);
conn = url.openConnection();
} catch (IOException e) {
log.info("环信-下载聊天记录:{}失败:", startTime);
e.printStackTrace();
}
//解压
try (InputStream inStream = conn.getInputStream();
GZIPInputStream gzip = new GZIPInputStream(inStream);
InputStreamReader reader = new InputStreamReader(gzip);
BufferedReader in = new BufferedReader(reader)) {
String read;
while ((read = in.readLine()) != null) {
//一行一条消息
messageList.add(read);
}
} catch (IOException e) {
log.info("环信-解压聊天记录:{}失败:", startTime);
e.printStackTrace();
}
return messageList;
}