将相对路径转为绝对路径的方法
来源:互联网 发布:ctr数据 编辑:程序博客网 时间:2024/05/18 19:40
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;
}
0 0
- 将相对路径转为绝对路径的方法
- 将网页的相对路径转为绝对路径
- Qt的相对路径转为绝对路径
- js 相对路径转为绝对路径
- 关于函数由绝对路径转为相对路径
- 将相对路径转绝对路径
- FCKeditor上传路径从绝对路径改成相对路径的方法 .
- javascript将相对路径转绝对路径的例子
- 绝对路径相对路径的比较
- 将网站绝对路径更改为相对路径
- 将网站绝对路径更改为相对路径
- C# 将绝对路径转换为相对路径
- C++和python中将程序内的绝对路径转为相对路径
- 在jsp中把相对路径变为绝对路径的方法
- 在web中将oad相对路径转为绝对路径(sping)
- jsp的路径(相对路径、绝对路径)
- 相对路径、根路径、绝对路径的区别
- JSP取得绝对路径和相对路径方法
- js判断对象属性,并追加
- js笔记(三)--正则表达式
- robot framework封装自定义关键字方法
- Linux Shell编程笔记
- 初探es6(react铺垫)
- 将相对路径转为绝对路径的方法
- wireless-tools源码分析-iwpriv
- 精通maven01
- 解读Secondary NameNode的功能
- Unittest命令行执行测试、执行测试发现操作实例
- 深度卷积对抗生成网络(DCGAN)
- 实现抽象类Dictionary
- HID设备类详解
- Linux网络管理命令