Java基于url获取host的两种方法

Java基于url获取host的两种方法

需求:

基于url获取host,case如下:

http://snv.iteye.com/blog/1992991结果为snv.iteye.com

snv.iteye.com/blog/1992991结果为snv.iteye.com

https://snv.iteye.com/blog/1992991结果为snv.iteye.com

http://snv.iteye.html结果为“”

http://snv.iteye.htm结果为“”

snv.iteye.html结果为“”

teye.html结果为“”

http://www.iteye.com/blog/1992991结果为www.iteye.com

www.iteye.com/blog/1992991结果为www.iteye.com

https://www.iteye.com/blog/1992991结果为www.iteye.com

基于URI或者URL

private static String getHost(String url) {  
    if (!(StringUtils.startsWithIgnoreCase(url, "http://") || StringUtils  
            .startsWithIgnoreCase(url, "https://"))) {  
        url = "http://" + url;  
    }  
    String returnVal = StringUtils.EMPTY;  
    try {  
        URI uri = new URI(url);  
        returnVal = uri.getHost();  
    } catch (Exception e) {  
    }  
    if ((StringUtils.endsWithIgnoreCase(returnVal, ".html") || StringUtils  
            .endsWithIgnoreCase(returnVal, ".htm"))) {  
        returnVal = StringUtils.EMPTY;  
    }  
    return returnVal;  
}  

基于正则

public static String getHost(String url) {  
    if (!(StringUtils.startsWithIgnoreCase(url, "http://") || StringUtils  
            .startsWithIgnoreCase(url, "https://"))) {  
        url = "http://" + url;  
    }  
  
    String returnVal = StringUtils.EMPTY;  
    try {  
        Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");  
        Matcher m = p.matcher(url);  
        if (m.find()) {  
            returnVal = m.group();  
        }  
  
    } catch (Exception e) {  
    }  
    if ((StringUtils.endsWithIgnoreCase(returnVal, ".html") || StringUtils  
            .endsWithIgnoreCase(returnVal, ".htm"))) {  
        returnVal = StringUtils.EMPTY;  
    }  
    return returnVal;  
}  

你可能感兴趣的:(Java,java,开发语言,jvm)