富文本的图片处理工具类
public class HtmlImg { private String content; private List<String> keys;//图片key //返回处理过的Json public String getJsonKeys() { return JSON.toJSONString(this.getKeys()); } /* * 编辑时处理富文本图片路径 * wmy 13:41 2019/5/10 * @Param [contentImgs] * @return java.lang.String **/ public String updateKeys(String contentImgs) { Set<String> imgImgs = new HashSet<>(); //之前从数据库里查出来的路径 if (contentImgs != null) { List<String> oldKeys = JSON.parseArray(contentImgs, String.class); if (oldKeys != null && oldKeys.size() > 0) { imgImgs.addAll(oldKeys); } } //新加入的路径 if (this.keys != null && this.keys.size() > 0) { imgImgs.addAll(this.keys); } return JSON.toJSONString(imgImgs); } }
/** * 替换富文本中的图片为域名地址 * @param htm * @return */ public static HtmlImg replaceBase64Img(String htm) { HtmlImg htmlImg = new HtmlImg(); String[] list = htm.split("<img src=\"data:image/png;base64,"); System.out.println(list.length); List<String> imgKeys = new ArrayList<>(); if(list.length>1){ String base64str,imgKey; StringBuilder sb = new StringBuilder(); sb.append(list[0]); for(int i=1;i<list.length;i++){ base64str = list[i].substring(0,list[i].indexOf("\">")); imgKey = ImageUtil.uploadImg(base64str); imgKeys.add(imgKey); sb.append("<img src=\""); sb.append(CommonConfig.STATIC_URL); sb.append(imgKey); sb.append(list[i].substring(list[i].indexOf("\">"))); } htmlImg.setContent(sb.toString()); htmlImg.setKeys(imgKeys); } else{ htmlImg.setContent(htm); } return htmlImg; }