jsoup中的相对路径怎么变成绝对路径
1个回答
展开全部
public static String relative2AbsolutePath(String content, String url, String tag, String property) throws URISyntaxException,
MalformedURLException {
String newContent = "";
if (content != null && content.trim() != "") {
URI base = new URI(url);// 基本网页URI
Document doc = Jsoup.parse(content);
for (Element ele : doc.getElementsByTag(tag)) {
String elePropValue = ele.attr(property);
if (!elePropValue.matches("^(https?|ftp):(\\\\|//).*$")) {
URI abs = base.resolve(elePropValue);// 解析相对URL,得到绝对URI
ele.attr(property, abs.toURL().toString());
}
}
newContent = doc.html();
}
return newContent;
}
public static String relative2AbsolutePath(String content, String url) {
try {
content = Tool.relative2AbsolutePath(content, url, "a", "href");
content = Tool.relative2AbsolutePath(content, url, "img", "src");
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
} catch (URISyntaxException e) {
log.error(e.getMessage(), e);
}
return content;
}
MalformedURLException {
String newContent = "";
if (content != null && content.trim() != "") {
URI base = new URI(url);// 基本网页URI
Document doc = Jsoup.parse(content);
for (Element ele : doc.getElementsByTag(tag)) {
String elePropValue = ele.attr(property);
if (!elePropValue.matches("^(https?|ftp):(\\\\|//).*$")) {
URI abs = base.resolve(elePropValue);// 解析相对URL,得到绝对URI
ele.attr(property, abs.toURL().toString());
}
}
newContent = doc.html();
}
return newContent;
}
public static String relative2AbsolutePath(String content, String url) {
try {
content = Tool.relative2AbsolutePath(content, url, "a", "href");
content = Tool.relative2AbsolutePath(content, url, "img", "src");
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
} catch (URISyntaxException e) {
log.error(e.getMessage(), e);
}
return content;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询