解决图片base64过大导致前端加载缓慢

Java正则替换图片base64 为 图片链接

 public static void test34() throws IOException {
        String str = "<p align=\"\" style=\"margin-top: 0px; text-indent: 2em; text-align: justify; font-size: 16pt;\">xx。<o:p></o:p></p>\n<p style=\"margin-top: 0px; text-align: center; font-size: 16pt;\"><img src=\"data:image/jpeg;base64,fhtykrglkfgfk774747474747474555465465ftgjhjgnghjnmgh\" data-originalurl=\"/xx/87f5444c6e2e4c4ea9a6e065d0164085.jpg\" hspace=\"\" border=\"0\" align=\"center\"></p>\n<p align=\"\" style=\"margin-top: 0px; text-indent: 2em; text-align: justify; font-size: 16pt;\">xx。<o:p></o:p></p>\n<p style=\"margin-top: 0px; text-align: center; font-size: 16pt;\"><img src=\"data:image/png;base64,hgjkgujgjhbjkjhkjh2345346464165gbjgbkjhjbkhk\" data-originalurl=\"/mongo2/3f6093accd494704bcb6c5cc3df16706.jpg\" hspace=\"\" border=\"0\" align=\"center\"></p>\n<p align=\"\" style=\"margin-top: 0px; text-indent: 2em; text-align: justify; font-size: 16pt;\">xx。<o:p></o:p></p>\n<p align=\"\" style=\"margin-top: 0px; text-indent: 2em; text-align: justify; font-size: 16pt;\">xx<o:p></o:p></p>\n<p align=\"\" style=\"margin-top: 0px; text-indent: 2em; text-align: justify; font-size: 16pt;\">xx<o:p></o:p></p>\n<p align=\"\" style=\"margin-top: 0px; text-indent: 2em; text-align: justify; font-size: 16pt;\">xx<o:p></o:p></p>";
        String regex = "<img src=\"data:image/(\\S+);base64,(\\S+)\"";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            String key = matcher.group(2);
            System.out.println("key:==========="+key);
            String base64 = matcher.group(1)+ "88888888888888888888888888888888888";
            //将要替换的地方先进行缓存
            matcher.appendReplacement(sb, "<img src=\""+base64+"\"");
        }
        //再将不同部分缓存
        matcher.appendTail(sb);
        System.out.println("Contents of the StringBuffer: \n"+ sb.toString() );
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值