环信拉取聊天记录并解析
环信返回的聊天记录文件下载地址下载的文件是.gz格式的压缩文件。如果直接下载的话输出流创建的文件必须是.gz格式压缩文件,否则会乱码。我这里是直接下载、解压、写入内存、读取写入内存的文件、转换字符串、解析json字符串为对象(解析json字符串使用了lombok和GsonFormat插件生成ChatMessageHistory类和DownLoadMessageUrl类)一起完成的。
代码实现
public void getMessage(String date){ OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .url("https://a1.easemob.com/#{Orgname}/#{Appname}/chatmessages/"+date) .header("Content-Type","application/json") .header("Authorization","Bearer #{token}") .get() .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { System.out.println("onFailure3: " + e.getMessage()); }
@Override public void onResponse(Call call, Response response) throws IOException { System.out.println(response.protocol() + ":" +response.code() + ": " + response.message()); Headers headers = response.headers(); for (int i = 0; i < headers.size(); i++) { System.out.println(headers.name(i) + ":" + headers.value(i)); } String messageUrlJson = response.body().string(); DownLoadMessageUrl downLoadMessageUrl = JSON.parseObject(messageUrlJson,DownLoadMessageUrl.class); System.out.println("解析后得到的地址:"+downLoadMessageUrl.getData().get(0).getUrl()); String messageFileUrl = downLoadMessageUrl.getData().get(0).getUrl(); downLoadFile(messageFileUrl,date); } }); }
public void downLoadFile(String messageFileUrl,String date){ String sourcedir = "G:\\test\\"+date+".gz";
OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder() .url(messageFileUrl) .get() .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { System.out.println("downLoadFile: " + e.getMessage()); }
@Override public void onResponse(Call call, Response response) throws IOException { System.out.println(response.protocol() + ":" +response.code() + " : " + response.message()); Headers headers = response.headers(); for (int i = 0; i < headers.size(); i++) { System.out.println(headers.name(i) + ":" + headers.value(i)); } unGzipFile(sourcedir,response); } }); }
public void unGzipFile(String sourcedir,Response response) { String ouputfile = ""; InputStream fin = null; GZIPInputStream gzin = null; FileOutputStream fout = null; try { fin=response.body().byteStream(); gzin = new GZIPInputStream(fin); ouputfile = sourcedir.substring(0,sourcedir.lastIndexOf('.')); System.out.println("解压后的文件路径及名称:"+ouputfile); fout = new FileOutputStream(ouputfile); int num; byte[] buf=new byte[2048]; while ((num = gzin.read(buf,0,buf.length)) != -1) { fout.write(buf,0,num); } System.out.println("解压完成"); getHistoryMessages(ouputfile); } catch (Exception ex){ System.out.println(ex.toString()); }finally { try { if (gzin != null){ gzin.close(); } if (fout != null){ fout.close(); } if (fin != null){ fin.close(); } } catch (IOException e) { e.printStackTrace(); } } return; }
public void getHistoryMessages(String fileName){ InputStream inputStream = null; try { inputStream = new FileInputStream(fileName); String msg = getStreamToStr(inputStream); System.out.println("解压后的消息字符串:"+msg); String str1 = msg.replace("{\"chat_type\""," {\"chat_type\""); String str2 = str1.trim(); String[] strings = str2.split(" "); List<ChatMessageHistory> chatMessageHistoryList = new ArrayList<>(); for (int i = 0; i < strings.length; i++) { ChatMessageHistory chatMessageHistory = JSON.parseObject(strings[i],ChatMessageHistory.class); chatMessageHistoryList.add(chatMessageHistory); } for (int i = 0; i < chatMessageHistoryList.size(); i++) { System.out.println("第"+(i+1)+"条消息:"+chatMessageHistoryList.get(i).getPayload().getBodies().get(0).getMsg()); } }catch (IOException e){ e.printStackTrace(); }finally { if (inputStream != null){ try { inputStream.close(); }catch (IOException e){ e.printStackTrace(); } } } }
public String getStreamToStr(InputStream tInputStream) { if (tInputStream != null) { InputStreamReader inputStreamReader = null; BufferedReader tBufferedReader = null; try { inputStreamReader = new InputStreamReader(tInputStream); tBufferedReader = new BufferedReader(inputStreamReader); StringBuffer tStringBuffer = new StringBuffer(); String sTempOneLine; while ((sTempOneLine = tBufferedReader.readLine()) != null) { tStringBuffer.append(sTempOneLine); } return tStringBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); }finally { try { if (inputStreamReader != null){ inputStreamReader.close(); } if (tBufferedReader != null){ tBufferedReader.close(); } }catch (IOException e){ e.printStackTrace(); } } } return null; }
|
(注意:#{Orgname}、#{Appname}、#{token}是变量,替换成自己的即可)
(注意:InputStream字节输入流是不能复用的,具体知识自行百度补充)