繁体   English   中英

使用Java将带有相对URL的锚标记转换为HTML内容中的绝对URL

[英]Converting anchor tag with relative URL to absolute URL in HTML content using Java

情况:

在服务器A上,我们要在服务器A上在线显示来自服务器B的内容。

问题:

服务器B上的内容中的某些超链接是相对于服务器B的,这使得它们在服务器A上显示时无效。

给定一段HTML代码,其中包含如下所示的定位标记

<a href="/something/somwhere.html">Somewhere</a>

将它们转换为最有效的方法是什么

<a href="http://server-b.com/something/somewhere.html">Somewhere</a>

内容中可以有多个锚标记,一个要注意的是某些标记可​​能是绝对的,我想保留这些标记,我只想在服务器B的域之前添加相对URL

根据周围很多你的web应用程序的设置方式,以及您的有效定义的东西,这可能不是你所需要的或所期待的。 但无论如何,如果你有你的HTML作为一个字符串(在例如过滤器的一些晚期),你可以这样做:

html = html.replaceAll("href=\"/", "href=\"http://server-b.com/")

有一种方法,我用它来将相对URL转换为绝对URL。 我用它来将某些页面转换为电子邮件正文。

public String replaceLinks(String address, String content) throws URISyntaxException{
    //absolute URI used for change all relative links
    URI addressUri = new URI(address);
    //finds all link atributes (href, src, etc.)
    Pattern pattern = Pattern.compile("(href|src|action|background)=\"[^\"]*\"", Pattern.CASE_INSENSITIVE);
    Matcher m = pattern.matcher(content);
    //determines if the link is allready absolute
    Pattern absoluteLinkPattern = Pattern.compile("[a-z]+://.+");
    //buffer for result saving
    StringBuffer buffer = new StringBuffer();
    //position from where should next interation take content to append to buffer
    int lastEnd = 0;
    while(m.find()){
        //position of link in quotes
        int startPos = content.indexOf('"',m.start())+1;
        int endPos = m.end()-1;
        String link = content.substring(startPos,endPos);
        Matcher absoluteMatcher = absoluteLinkPattern.matcher(link);
        //is the link relative?
        if(!absoluteMatcher.find())
        {
            //create relative URL
            URI tmpUri = addressUri.resolve(link);
            //append the string between links
            buffer.append(content.substring(lastEnd,startPos-1));
            //append new link
            buffer.append(tmpUri.toString());
            lastEnd =endPos+1;
        }
    }
    //append the end of file
    buffer.append(content.substring(lastEnd));
    return buffer.toString();
}

希望能帮助到你。

我不会用Java做到这一点。 我喜欢在视图层中处理特定于视图的逻辑。 我假设此代码块来自AJAX调用。 因此,您可以做的是从AJAX调用中获取HTML,然后执行以下操作:

jQuery(html).find("a[href]").each(function(index, value) {
  var $a = jQuery(value);
  var href = $a.attr("href");

  if(!/^http:/.test(href)) {
     $a.attr("href", "http://server-b.com" + href);
   }
});

或者,如果您真的想用Java做到这一点,Lauri的答案将起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM