【java】实现存储腾讯IM所有的历史聊天记录到本地服务器

【java】实现存储腾讯IM所有的历史聊天记录到本地服务器

1.需求描述 

        咱们使用腾讯IM及时通讯API, 只能保存7天的群聊记录,就算升级API最多也只能存储30天的。如果想永久存储聊天记录,该怎么办呢?下面是实现永久存储腾讯IM群聊记录到本地服务器的需求实现步骤。

2.需求调研

        1. 查看腾讯IM的API,只有一个获取群7天内聊天记录的接口,并且每次只能获取20条,需要循环获取,才能拿到7天内所有记录。

        2.获取到聊天记录后,聊天记录中的文件如:图片、视频等,可能也会随聊天记录过期而失效

3.需求实现

        1.实现一个方法,循环获取7天内的所有聊天记录,同时,对每条聊天记录进行遍历,如果是存在媒体文件:图片等,把图片另存为到云存储中,生成新的URL,替换聊天记录中的文件URL

        2.将本次获取的所有聊天记录存储在json文件中,进行云存储,本地记录json文件URL,所属的群聊ID,聊天记录开始时间,结束时间,序列号。方便前端根据序列号查询,按顺序读取json记录。

        3.创建定时任务,设置每7天执行一次(实际执行间隔小于7天,保证获取的记录不会断层),保证能获取完整的聊天记录。这样每7天就能保存一个聊天记录的json文件。

4.主要架构

      SpringBoot ,Minio ,mysql ,JDK1.8,Spring-quartz

5.准备工作

1.思路: 循环拿到聊天记录,解析,存储到json,保存到minio,存储保存记录

2.腾讯IM获取聊天记录接口: 

 3.返回值示例

IM返回的消息类型主要有四种:TIMTextElem(文本类型)、TIMImageElem(图片类型)、TIMVideoFileElem(视频类型)、TIMFileElem(文件类型)

格式如下:

[{"ActionStatus":"OK","ErrorCode":0,"ErrorInfo":"","GroupId":"32453925","IsFinished":1,"RspMsgList":[{"From_Account":"liang_xiao_yu02163","IsPlaceMsg":0,"MsgBody":[{"MsgContent":{"Download_Flag":2,"FileName":"新建文本文档.html","FileSize":15,"UUID":"1400386956-liang_xiao_yu02163-YY7npohmbQ3TkK5oj33YHY3JUSPp0JzR","Url":"http://fdbaad7b7ad11156a9345f9c0ce09ccc-%25E6%2596%25B0%25E5%25BB%25BA%25E6%2596%2587%25E6%259C%25AC%25E6%2596%2587%25E6%25A1%25A3.html"},"MsgType":"TIMFileElem"}],"MsgPriority":2,"MsgRandom":67439734,"MsgSeq":112,"MsgTimeStamp":1615532610},{"From_Account":"liang_xiao_yu02163","IsPlaceMsg":0,"MsgBody":[{"MsgContent":{"Text":"Fgg"},"MsgType":"TIMTextElem"}],"MsgPriority":2,"MsgRandom":3848154861,"MsgSeq":106,"MsgTimeStamp":1615425082},{"From_Account":"@TIM#SYSTEM","IsPlaceMsg":0,"MsgBody":{"MsgOperatorMemberExtraInfo":{"Role":0,"UserId":"administrator","User_Account":"administrator"},"MsgGroupNewInfo":{"GroupName":"ttt_Asia"},"OpType":6,"Operator_Account":"administrator"},"MsgPriority":2,"MsgRandom":521393613,"MsgSeq":105,"MsgTimeStamp":1615381923},{"From_Account":"liang_xiao_yu02163","IsPlaceMsg":0,"MsgBody":[{"MsgContent":{"Text":"YCYUP NCNBB"},"MsgType":"TIMTextElem"}],"MsgPriority":2,"MsgRandom":71490868,"MsgSeq":104,"MsgTimeStamp":1615380723},{"From_Account":"liang_xiao_yu02163","IsPlaceMsg":0,"MsgBody":[{"MsgContent":{"ImageFormat":3,"ImageInfoArray":[{"Height":1025,"Size":91694,"Type":1,"URL":"http://1615860237_1.PNG","Width":1652},{"Height":0,"Size":0,"Type":2,"URL":"http://1615860237_2.PNG","Width":0},{"Height":198,"Size":0,"Type":3,"URL":"http:///1615860237_3.PNG","Width":320}],"UUID":"1400386956-liang_xiao_yu02163-AtsNECl1HP55DokcCSAsneqqIJ9pyaku"},"MsgType":"TIMImageElem"}],"MsgPriority":2,"MsgRandom":99829389,"MsgSeq":95,"MsgTimeStamp":1615377696},{"From_Account":"liang_xiao_yu02163","IsPlaceMsg":0,"MsgBody":[{"MsgContent":{"ThumbDownloadFlag":2,"ThumbFormat":"png","ThumbHeight":200,"ThumbSize":1668,"ThumbUUID":"1400386956-liang_xiao_yu02163-4WJCh1B3Bhnx0prAMKwT4ASeLOArpF1Z","ThumbUrl":"","ThumbWidth":200,"VideoDownloadFlag":2,"VideoFormat":"mp4","VideoSecond":0,"VideoSize":3373894,"VideoUUID":"1400386956-liang_xiao_yu02163-V0i81DKCmvWPwPFP43Hsr7i30YA7DgJX","VideoUrl":"/f555f6ad99f32106712600580650ce3e-2de58c2f5a97584418de6752f1668aad.mp4"},"MsgType":"TIMVideoFileElem"}],"MsgPriority":2,"MsgRandom":14396311,"MsgSeq":94,"MsgTimeStamp":1615377647}]}
]

6. 具体实现

    PS:因为不仅仅要存储文本聊天记录,还有存储聊天记录里的图片,视频,文件等,所以我们需要解析之后,把里面的文件自己使用云存储存起来,再将记录中的url替换成我们自己存储的url。

大概流程:

请求API拿到聊天记录 -> 将记录中的文件自己云存储,url替换成云存储url -> 最后将记录存到json文件,上传云存储 -> 保存存储记录到mysql -> 查询mysql,根据url解析json文件 -> 就能读到聊天记录了

1.构建解析对象:

调API接收的结果对象:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ImResult implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty("请求处理的结果,OK 表示处理成功,FAIL 表示失败")@JsonProperty(value="ActionStatus")@JSONField(name = "ActionStatus")private String ActionStatus;@ApiModelProperty("错误码,0表示成功,非0表示失败")@JsonProperty(value="ErrorCode")@JSONField(name = "ErrorCode")private Integer ErrorCode;@ApiModelProperty("错误信息")@JsonProperty(value="ErrorInfo")@JSONField(name = "ErrorInfo")private String ErrorInfo;@ApiModelProperty("请求中的群组 ID")@JsonProperty(value="GroupId")@JSONField(name = "GroupId")private String GroupId;@ApiModelProperty("是否完成获取所有消息:0未完成 1已完成")@JsonProperty(value="IsFinished")@JSONField(name = "IsFinished")private Integer IsFinished;@ApiModelProperty("返回的消息列表")@JsonProperty(value="RspMsgList")@JSONField(name = "RspMsgList")private List<ImMsgBody> RspMsgList;}

消息列表对象

/*** @author:yuchen* @createTime:2021/3/11 10:38*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ImMsgBody<T> implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty("消息的发送者")@JsonProperty(value="From_Account")@JSONField(name = "From_Account")private String From_Account;@ApiModelProperty("MsgBody 为空,该字段为1,撤回的消息,该字段为2")@JsonProperty(value="IsPlaceMsg")@JSONField(name = "IsPlaceMsg")private Integer IsPlaceMsg;@ApiModelProperty("消息内容")@JsonProperty(value="MsgBody")@JSONField(name = "MsgBody")private T MsgBody;@ApiModelProperty("消息的优先级")@JsonProperty(value="MsgPriority")@JSONField(name = "MsgPriority")private Integer MsgPriority;@ApiModelProperty("消息随机值,用于对消息去重")@JsonProperty(value="MsgRandom")@JSONField(name = "MsgRandom")private Long MsgRandom;@ApiModelProperty("消息 seq,用于标识唯一消息,值越小发送的越早")@JsonProperty(value="MsgSeq")@JSONField(name = "MsgSeq")private Integer MsgSeq;@ApiModelProperty("消息被发送的时间戳,server 的时间")@JsonProperty(value="MsgTimeStamp")@JSONField(name = "MsgTimeStamp")private Long MsgTimeStamp;}

消息体对象

/*** @author:yuchen* @createTime:2021/3/11 10:38*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class MsgBody<T> implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty("消息类型")@JsonProperty(value="MsgType")@JSONField(name = "MsgType")private String MsgType;@ApiModelProperty("消息内容")@JsonProperty(value="MsgContent")@JSONField(name = "MsgContent")private T MsgContent;}

消息内容对象

图片内容对象

/*** @author:yuchen* @createTime:2021/3/11 10:38*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ImgMsgContent implements Serializable {private static final long serialVersionUID = 1L;@JsonProperty(value="ImageInfoArray")@JSONField(name = "ImageInfoArray")private List<ImgMsg> ImageInfoArray;@JsonProperty(value="UUID")@JSONField(name = "UUID")private String UUID;@JsonProperty(value="ImageFormat")@JSONField(name = "ImageFormat")private Integer ImageFormat;}
/*** @author:yuchen* @createTime:2021/3/11 10:38*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class ImgMsg implements Serializable {private static final long serialVersionUID = 1L;@JsonProperty(value="Type")@JSONField(name = "Type")private Integer Type;@JsonProperty(value="Size")@JSONField(name = "Size")private Integer Size;@JsonProperty(value="Height")@JSONField(name = "Height")private Integer Height;@JsonProperty(value="Width")@JSONField(name = "Width")private Integer Width;@JsonProperty(value="URL")@JSONField(name = "URL")private String URL;}

视频内容对象


/*** @author:yuchen* @createTime:2021/3/11 10:38* 视频聊天记录消息结构*/
@Data
public class VedioMsgContent implements Serializable {private static final long serialVersionUID = 1L;@JsonProperty(value = "ThumbFormat")@JSONField(name = "ThumbFormat")private String ThumbFormat;@JsonProperty(value = "ThumbSize")@JSONField(name = "ThumbSize")private Long ThumbSize;@JsonProperty(value = "ThumbUUID")@JSONField(name = "ThumbUUID")private String ThumbUUID;@JsonProperty(value = "VideoDownloadFlag")@JSONField(name = "VideoDownloadFlag")private Integer VideoDownloadFlag;@JsonProperty(value = "ThumbUrl")@JSONField(name = "ThumbUrl")private String ThumbUrl;@JsonProperty(value = "VideoFormat")@JSONField(name = "VideoFormat")private String VideoFormat;@JsonProperty(value = "VideoUrl")@JSONField(name = "VideoUrl")private String VideoUrl;@JsonProperty(value = "VideoUUID")@JSONField(name = "VideoUUID")private String VideoUUID;@JsonProperty(value = "VideoSecond")@JSONField(name = "VideoSecond")private Integer VideoSecond;@JsonProperty(value = "ThumbDownloadFlag")@JSONField(name = "ThumbDownloadFlag")private Integer ThumbDownloadFlag;@JsonProperty(value = "VideoSize")@JSONField(name = "VideoSize")private Long VideoSize;@JsonProperty(value = "ThumbHeight")@JSONField(name = "ThumbHeight")private Integer ThumbHeight;@JsonProperty(value = "ThumbWidth")@JSONField(name = "ThumbWidth")private Integer ThumbWidth;
}

文件内容对象

/*** @author:yuchen* @createTime:2021/3/11 10:38*/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class FileMsgContent implements Serializable {private static final long serialVersionUID = 1L;@JsonProperty(value="Download_Flag")@JSONField(name = "Download_Flag")private Integer Download_Flag;@JsonProperty(value="FileName")@JSONField(name = "FileName")private String FileName;@JsonProperty(value="Url")@JSONField(name = "Url")private String Url;@JsonProperty(value="UUID")@JSONField(name = "UUID")private String UUID;@JsonProperty(value="FileSize")@JSONField(name = "FileSize")private Long FileSize;
}

2 调API获取聊天记录

public ImResult getGroupMsg(String groupId, Integer reqMsgSeq) {ObjectNode object = JsonNodeFactory.instance.objectNode();object.put("GroupId", groupId);if (null != reqMsgSeq && 0 != reqMsgSeq) {object.put("ReqMsgSeq", reqMsgSeq);}object.put("ReqMsgNumber", 20);log.info(object.toString());HttpResult httpResult = null;try {httpResult = restApiClient.makeRestApiRequest(object.toString(), ContantIMUrl.generateAPI(ContantIMUrl.METHOD_GET_MSG_BY_GROUP));} catch (Exception e) {log.error("getGroupMsg,Error:", e);}return JSON.parseObject(JSON.toJSONBytes(JSON.parse(httpResult.getBody())), ImResult.class);}

3. 循环获取所有的聊天记录

public List<ImResult> getGroupMsgAll(String groupId) {List<ImResult> results = new ArrayList<>();int reqMsgSeq = 0;boolean flag = false;List<ImMsgBody> rspMsgList;ImResult result;do {result = getGroupMsg(groupId, reqMsgSeq);rspMsgList = result.getRspMsgList();if (rspMsgList != null) {// 移除指定对象rspMsgList.removeIf(imMsgBody -> imMsgBody.getIsPlaceMsg() != 0);int msgSize = rspMsgList.size();if (msgSize != 0) {// 遍历消息,如果消息是图片,视频就保存文件服务器,返回地址,重新存进去rspMsgList = saveImgOrVideo(rspMsgList, groupId);result.setRspMsgList(rspMsgList);results.add(result);if (msgSize > 1) {reqMsgSeq = rspMsgList.get(msgSize - 1).getMsgSeq() - 1;if(reqMsgSeq == 0){// 说明消息读完了,跳出循环flag = false;}else {// 否则继续读flag = true;}} else {flag = false;}} else {flag = false;}}} while (flag);return results;}

4.获取聊天记录的过程中,存储图片,视频,文件

private List<ImMsgBody> saveImgOrVideo(List<ImMsgBody> rspMsgList, String groupId) {rspMsgList.forEach(imMsgBody -> {try {List<MsgBody> msgBodys = JSONObject.parseArray(JSONArray.toJSONString(imMsgBody.getMsgBody()), MsgBody.class);if (msgBodys != null && msgBodys.get(0) != null) {MsgBody msgBody = msgBodys.get(0);String msgType = msgBody.getMsgType();if (msgType != null) {if ("TIMImageElem".equalsIgnoreCase(msgType)) {log.info("存图片");ImgMsgContent img = JSON.parseObject(JSON.toJSONBytes(msgBody.getMsgContent()), ImgMsgContent.class);List<ImgMsg> img1 = img.getImageInfoArray();img1.forEach(imgMsg -> {FileInputStream fileInputStream = null;File fileByUrl = null;try {// 根据url下载图片fileByUrl = FileUtil.getFileByUrl(imgMsg.getURL());fileInputStream = new FileInputStream(fileByUrl);String newName = DateUtil.currentSeconds() + "_" + imgMsg.getType() + FileUtil.getFileType(imgMsg.getURL());String fileObject = Contant.IM_IMG + groupId + "/" + newName;// 存储图片minioUtil.putObject(minioConfig.getBucketName(), fileObject, fileInputStream);String url = minioUtil.getObjectUrl(minioConfig.getBucketName(), fileObject);log.info("Minio存储Img");// 更新图片地址imgMsg.setURL(url);} catch (Exception ignored) {// 如果出异常了,就不更新聊天记录的图片了} finally {if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException ignored) {}}if (fileByUrl != null) {fileByUrl.delete();}}});img.setImageInfoArray(img1);msgBody.setMsgContent(img);msgBodys.clear();msgBodys.add(msgBody);imMsgBody.setMsgBody(msgBodys);} else if ("TIMVideoFileElem".equalsIgnoreCase(msgType)) {log.info("存视频");VedioMsgContent img = JSON.parseObject(JSON.toJSONBytes(msgBody.getMsgContent()), VedioMsgContent.class);FileInputStream fileInputStream = null;File fileByUrl = null;try {// 根据url下载图片fileByUrl = FileUtil.getFileByUrl(img.getThumbUrl());fileInputStream = new FileInputStream(fileByUrl);String newName = DateUtil.currentSeconds() + "_Thumb" + FileUtil.getFileType(img.getThumbUrl());String fileObject = Contant.IM_IMG + groupId + "/" + newName;// 存储图片minioUtil.putObject(minioConfig.getBucketName(), fileObject, fileInputStream);String url = minioUtil.getObjectUrl(minioConfig.getBucketName(), fileObject);log.info("Minio存储缩略图");img.setThumbUrl(url);try {fileInputStream.close();} catch (IOException ignored) {}fileByUrl.delete();// 根据url下载图片fileByUrl = FileUtil.getFileByUrl(img.getVideoUrl());fileInputStream = new FileInputStream(fileByUrl);String newMp4Name = DateUtil.currentSeconds() + FileUtil.getFileType(img.getVideoUrl());String mp4Object = Contant.IM_VEDIO + groupId + "/" + newMp4Name;// 存储图片minioUtil.putObject(minioConfig.getBucketName(), mp4Object, fileInputStream);String urlMp4 = minioUtil.getObjectUrl(minioConfig.getBucketName(), mp4Object);log.info("Minio存储vedio");img.setVideoUrl(urlMp4);} catch (Exception ignored) {// 如果出异常了,就不更新聊天记录的文件} finally {if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException ignored) {}}if (fileByUrl != null) {fileByUrl.delete();}}msgBody.setMsgContent(img);msgBodys.clear();msgBodys.add(msgBody);imMsgBody.setMsgBody(msgBodys);} /*else if ("TIMFileElem".equalsIgnoreCase(msgType)) {log.info("存文件");FileMsgContent file = JSON.parseObject(JSON.toJSONBytes(msgBody.getMsgContent()), FileMsgContent.class);FileInputStream fileInputStream = null;File fileByUrl = null;try {// 根据url下载文件fileByUrl = FileUtil.getFileByUrl(file.getUrl());fileInputStream = new FileInputStream(fileByUrl);String fileObject = Contant.IM_FILE + groupId + "/" + fileByUrl.getName();// 存储图片minioUtil.putObject(minioConfig.getBucketName(), fileObject, fileInputStream);String url = minioUtil.getObjectUrl(minioConfig.getBucketName(), fileObject);log.info("Minio存储文件");file.setUrl(url);} catch (Exception ignored) {// 如果出异常了,就不更新聊天记录的文件} finally {if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException ignored) {}}if (fileByUrl != null) {fileByUrl.delete();}}msgBody.setMsgContent(file);msgBodys.clear();msgBodys.add(msgBody);imMsgBody.setMsgBody(msgBodys);}*/}}} catch (Exception ignored) {// 报错说明消息不符合解析格式,不需要处理}});return rspMsgList;}

5. 保存解析后的聊天记录

public SifarResult saveRecordById(String groupId) {log.info("群id = {}",groupId);SifarResult<GroupRecord> recordResult = customerService.getGroupRecordNew(groupId);if ("0".equals(recordResult.getStatu())) {List<ImResult> groupMsgAll = getGroupMsgAll(groupId);log.info("群id = {},获取聊天记录success",groupId);// 保存聊天记录int size = groupMsgAll.size();if (size > 0) {GroupRecord r1 = recordResult.getContent();log.info("计算seq={}", r1);int seq = 1;if (r1 != null) {seq = r1.getRecordSeq() + 1;}try {// 保存聊天记录String url = saveRecord(groupMsgAll, groupId);log.info("groupId={},聊天记录 url={}",groupId, url);if (url != null) {// 聊天记录开始时间//最开始的消息列表List<ImMsgBody> rspMsg = groupMsgAll.get(size - 1).getRspMsgList();Long startTime = rspMsg.get(rspMsg.size() - 1).getMsgTimeStamp();log.info("聊天记录开始时间={}", startTime);// 聊天记录结束时间Long endTime = groupMsgAll.get(0).getRspMsgList().get(0).getMsgTimeStamp();log.info("聊天记录结束时间={}", endTime);r1 = new GroupRecord();r1.setGroupId(groupId);r1.setRecordUrl(url);r1.setRecordSeq(seq);r1.setStartTime(startTime);r1.setEndTime(endTime);r1.setCreateTime(TimeUtil.GetGreenDate());customerService.saveGroupRecord(r1);log.info("群={},保存聊天记录 success 。。。",groupId);}} catch (Exception e) {log.error("保存聊天记录异常群{},Error:", groupId, e);}}}return SifarResult.success();}

6.保存聊天记录到Minio

 private String saveRecord(List<ImResult> groupMsgAll, String groupId) {FileInputStream fileInputStream = null;File file = new File(groupId + ".json");try {FileOutputStream fileOutputStream = new FileOutputStream(file);OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);String jsonString = JSONObject.toJSONString(groupMsgAll);osw.write(jsonString);osw.flush();//清空缓冲区,强制输出数据osw.close();//关闭输出流fileOutputStream.close();fileInputStream = new FileInputStream(file);String newName = DateUtil.format(TimeUtil.GetGreenDate(),"yyyy-MM-dd_HH_mm_ss") + "_" + file.getName();String fileObject = Contant.IM_RECORD + groupId + "/" + newName;// 存储图片minioUtil.putObject(minioConfig.getBucketName(), fileObject, fileInputStream);return minioUtil.getObjectUrl(minioConfig.getBucketName(), fileObject);} catch (Exception e) {log.error("groupId={}保存聊天json文件异常,Error:{}",groupId, e);} finally {if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException ignored) {}}file.delete();}return null;}

7.保存聊天记录的对象

/*** @author yuchen* @since 2021-03-12*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="GroupRecord对象", description="")
public class GroupRecord implements Serializable {private static final long serialVersionUID=1L;@ApiModelProperty(value = "主键")private Integer id;@ApiModelProperty(value = "群号")private String groupId;@ApiModelProperty(value = "聊天记录文件地址")private String recordUrl;@ApiModelProperty(value = "聊天记录文件seq")private Integer recordSeq;@ApiModelProperty(value = "0不能看 1可以看")private Integer browse;@ApiModelProperty(value = "聊天记录开始时间")private Long startTime;@ApiModelProperty(value = "聊天记录结束时间")private Long endTime;@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")@ApiModelProperty(value = "创建时间")private Date createTime;}

 8.FileUtil

package com.yuchenmon.utils;import lombok.extern.slf4j.Slf4j;import java.io.*;
import java.HttpURLConnection;
import java.URL;/*** @author:yuchen* @createTime:2021/3/12 10:42*/
@Slf4j
public class FileUtil {public static File getFileInByUrl(String fileUrl) {ByteArrayOutputStream outStream = new ByteArrayOutputStream();BufferedOutputStream stream = null;InputStream inputStream = null;FileOutputStream fileOutputStream = null;File file = null;HttpURLConnection conn = null;try {String imgName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);URL imageUrl = new URL(fileUrl);conn = (HttpURLConnection) imageUrl.openConnection();
//			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");//设置请求头conn.setRequestProperty("Charsert", "UTF-8");conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");//设置参数类型是json格式conn.setRequestProperty("Connection", "Keep-Alive");inputStream = conn.getInputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inputStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}file = new File(imgName);fileOutputStream = new FileOutputStream(file);stream = new BufferedOutputStream(fileOutputStream);stream.write(outStream.toByteArray());} catch (Exception e) {log.error("获取文件,Error", e);} finally {if (stream != null) {try {stream.close();} catch (IOException ignored) {}}if (fileOutputStream != null) {try {fileOutputStream.close();} catch (IOException ignored) {}}try {outStream.close();} catch (IOException ignored) {}if (inputStream != null) {try {inputStream.close();} catch (IOException ignored) {}}if (conn != null) {conn.disconnect();}}return file;}public static InputStream getInputByUrl(String fileUrl) {InputStream inputStream = null;HttpURLConnection conn = null;try {URL imageUrl = new URL(fileUrl);conn = (HttpURLConnection) imageUrl.openConnection();conn.setRequestProperty("Charsert", "UTF-8");conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");//设置参数类型是json格式conn.setRequestProperty("Connection", "Keep-Alive");inputStream = conn.getInputStream();} catch (Exception e) {log.error("获取文件,Error", e);}finally {if (conn != null) {conn.disconnect();}}return inputStream;}public static File getFileByUrl(String fileUrl) {ByteArrayOutputStream outStream = new ByteArrayOutputStream();BufferedOutputStream stream = null;InputStream inputStream = null;FileOutputStream fileOutputStream = null;File file = null;HttpURLConnection conn = null;try {String imgName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);URL imageUrl = new URL(fileUrl);conn = (HttpURLConnection) imageUrl.openConnection();
//			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");//设置请求头conn.setRequestProperty("Charsert", "UTF-8");conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");//设置参数类型是json格式conn.setRequestProperty("Connection", "Keep-Alive");inputStream = conn.getInputStream();byte[] buffer = new byte[1024];int len = 0;while ((len = inputStream.read(buffer)) != -1) {outStream.write(buffer, 0, len);}file = new File(imgName);fileOutputStream = new FileOutputStream(file);stream = new BufferedOutputStream(fileOutputStream);stream.write(outStream.toByteArray());} catch (Exception e) {log.error("获取文件,Error", e);} finally {if (stream != null) {try {stream.close();} catch (IOException ignored) {}}if (fileOutputStream != null) {try {fileOutputStream.close();} catch (IOException ignored) {}}try {outStream.close();} catch (IOException ignored) {}if (inputStream != null) {try {inputStream.close();} catch (IOException ignored) {}}if (conn != null) {conn.disconnect();}}return file;}public static String getFileType(String fileUrl) {String fileType = fileUrl.substring(fileUrl.lastIndexOf("."));// 如果从url获取的文件类型长度大于7个,说明不是正常文件类型if (fileType.length() > 7) {if (fileType.contains("jpg")) {fileType = ".jpg";} else if (fileType.contains("PNG")) {fileType = ".PNG";} else {fileType = ".PNG";}}return fileType;}public static void deleteAllFile(File rootFile) {if(rootFile!=null){if(rootFile.isFile()){rootFile.delete();}else{File[] files = rootFile.listFiles();// 将非空文件夹转换成File数组for (File file : files) {//使用foreach语句遍历文件数组deleteAllFile(file);// 删除指定文件夹下的所有非空文件夹(包括file)}rootFile.delete();// 删除指定文件夹下的所有空文件夹}}}public static String getFileNameByUrl(String fileUrl) {return fileUrl.substring(fileUrl.lastIndexOf("/")+1);}public static void main(String[] args) {String fileNameByUrl = getFileNameByUrl("http:///20210131DE11UM_PayVoucher_2021-02-04.jpg");System.out.println(fileNameByUrl);}}

        至此,咱们就能每7天,保存所有群聊的聊天记录到自己的服务器中,前端根据群聊ID,查询json文件记录表,读取josn文件回显即可。因为保存的格式是IM的标准格式,前端拿IM的demoUI就能渲染,不需要额外解析。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1698282673a854333.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信