将相对路径转为绝对路径的方法

来源:互联网 发布: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;
    }
0 0
原创粉丝点击