有个需求需要统计某台机器访问其他网站的频次,同一个主域名下的频次需要合并,即例如访问
mp.csdn.net 和 blog.csdn.net 各一次,应该统计为 csdn.net 两次。引入guava
https://mvnrepository.com/artifact/com.google.guava/guava
案例com.google.guava guava15.0 compile
private String dealHost(String host) { InternetDomainName domainName = InternetDomainName.from(host); String top = domainName.topPrivateDomain().toString(); return top; }测试
public static void mainHostTest(){ String urlstr = "https://abc.news.sina.com.cn/123/ref?free=key&kt=3"; try { URL url = new URL(urlstr); String host = url.getHost(); System.out.println("host: "+host); InternetDomainName domainName = InternetDomainName.from(host); String top = domainName.topPrivateDomain().toString(); System.out.println("top: "+top); } catch (Exception e) { e.printStackTrace(); } }结果
host: abc.news.sina.com.cn top: sina.com.cn