场景:解决富文本中上传本地图片后,临时图片地址处理。
xml
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
java
public static String documentBody(String newsBody) {
Element doc = Jsoup.parseBodyFragment(newsBody).body();
Elements pngs = doc.select("img[src]");
String httpHost = "http://192.168.1.1";
for (Element element : pngs) {
String imgUrl = element.attr("src");
if (imgUrl.trim().startsWith("/")) {
imgUrl = httpHost + imgUrl;
element.attr("src", imgUrl);
}
}
return newsBody = doc.toString();
}