超长文本消息回写企业微信端后台应用遭到截断

当向企业微信的自建应用推送消息时:消息内容最长不超过2048个字节,超过将截断。为此通过简单的拆分字符回写解决,解决方式如下

关键代码:根据非单词字符拆分字符串 String[] parts = content.split("(?<=\\W)");

private void writeResponse(Response response) {
    String content = response.getData().trim();
    if (StringUtils.hasText(content)) {
        //回写企业微信端。若接口返回的内容字节长度超过2048时,将会截断返回
        if (content.getBytes(StandardCharsets.UTF_8).length <= 2048) {
            wxWorkService.sendResponse(content);
        } else {
            StringBuilder sb = new StringBuilder();
            //根据非单词字符拆分字符串
            String[] parts = content.split("(?<=\\W)");
            for (String part : parts) {
                if ((sb + part).getBytes(StandardCharsets.UTF_8).length > 2048) {
                    //达到最大字节限制,先回写
                    wxWorkService.sendResponse(sb.toString());
                    //重新开始下一个部分
                    sb = new StringBuilder();
                }
                sb.append(part);
            }

            //回写最后一个部分的内容
            wxWorkService.sendResponse(sb.toString());
        }
    }
}

 

posted @   伊文小哥  阅读(290)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示