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);
elePropValue = elePropValue.replaceAll("..// ", "..//");
if(isContainChinese(elePropValue)){
System.out.println(elePropValue);
elePropValue = URLEncoder.encode(elePropValue).replaceAll("%3A", ":").replaceAll("%2F", "/");
System.out.println(elePropValue);
}
elePropValue = elePropValue.trim();
if(elePropValue.contains("javascript")){
continue;
}
if(elePropValue.contains("file:///")){
continue;
}
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 = relative2AbsolutePath(content, url, "img", "src");
content = relative2AbsolutePath(content, url, "a", "href");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return content;
}
public static boolean isContainChinese(String str) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}
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);
elePropValue = elePropValue.replaceAll("..// ", "..//");
if(isContainChinese(elePropValue)){
System.out.println(elePropValue);
elePropValue = URLEncoder.encode(elePropValue).replaceAll("%3A", ":").replaceAll("%2F", "/");
System.out.println(elePropValue);
}
elePropValue = elePropValue.trim();
if(elePropValue.contains("javascript")){
continue;
}
if(elePropValue.contains("file:///")){
continue;
}
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 = relative2AbsolutePath(content, url, "img", "src");
content = relative2AbsolutePath(content, url, "a", "href");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return content;
}
public static boolean isContainChinese(String str) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}