富文本转HTML字符串
最近工作中遇到需要将富文本内容在浏览器展示 写了个一个基于jsoup的转换工具类
依赖
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.3</version>
</dependency>
public class RichTextToHtmlConverter {
public static String convert(String title, String richText, String createTime) {
String result = preProcessImg(richText);
StringBuilder htmlBuilder = new StringBuilder(result);
int bodyTagIndex = result.indexOf("<body>");
if (-1 != bodyTagIndex) {
String titleStr = "<div style='text-align:center;'><h3 style='margin:auto;'>" + title + "</h3></div>" +
"<div style='text-align:center; height:15px;'><span style='float:right; color:gray;'><i>发布时间:</i>" + createTime + "</span></div>";
htmlBuilder.insert(bodyTagIndex + 6, titleStr);
}
return htmlBuilder.toString();
}
private static String preProcessImg(String richText) {
Document document = Jsoup.parse(richText);
Elements elements = document.getAllElements();
for (Element element : elements) {
if ("img".equalsIgnoreCase(element.tagName())) {
String imgPath = element.attr("src");
if (!StringUtil.isEmpty(imgPath)) {
String base64 = imgToBase64(imgPath);
element.attr("src", base64);
}
}
}
return document.outerHtml().replaceAll("\n", "");
}
private static String imgToBase64(String imagePath) {
BufferedImage image;
try {
image = ImgUtil.read(new URL(imagePath));
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return ImgUtil.toBase64DataUri(image, ImgUtil.IMAGE_TYPE_JPG);
}
}
如果遇到内容在转JSON串的时候被转义 使用 org.apache.commons.lang 包下的StringEscapeUtils.unescapeJavaScript() 转义一下即可