Get the second level domain of an URL (java)

前端 未结 10 919
长发绾君心
长发绾君心 2020-12-06 01:04

I am wondering if there is a parser or library in java for extracting the second level domain (SLD) in an URL - or failing that an algo or regex for doing the same. For exam

10条回答
  •  心在旅途
    2020-12-06 01:13

    public static String getTopLevelDomain(String uri) {
    
    InternetDomainName fullDomainName = InternetDomainName.from(uri);
    InternetDomainName publicDomainName = fullDomainName.topPrivateDomain();
    String topDomain = "";
    
    Iterator it = publicDomainName.parts().iterator();
    while(it.hasNext()){
        String part = it.next();
        if(!topDomain.isEmpty())topDomain += ".";
        topDomain += part;
    }
    return topDomain;
    }
    

    Just give the domain, and u will get the top level domain. download jar file from http://code.google.com/p/guava-libraries/

提交回复
热议问题