java解析URL中domain、端口和协议的两种方法
?
@Testpublic void parseDomain() throws IOException {for (int i = 0; i < 10000000; i++) {String urlAddress = "http://www.roseindia.net/jsf/JSFLoginApplication.shtml";URL url = new URL(urlAddress);assertEquals("www.roseindia.net", url.getHost());assertEquals("/jsf/JSFLoginApplication.shtml", url.getPath());assertEquals(-1, url.getPort()); // -1 if not setassertEquals("http", url.getProtocol());}}@Testpublic void parseDomainByStringUtils() {for (int i = 0; i < 10000000; i++) {String urlAddress = "http://www.roseindia.net/jsf/JSFLoginApplication.shtml";assertEquals("www.roseindia.net", StringUtils.split(urlAddress, ":/")[1]);assertEquals("http", StringUtils.split(urlAddress, ":/")[0]);}}
?
?
代码可读性比较:使用URL类更优雅
性能比较:URL类解析7s,?StringUtils解析使用18s
?
why not URL class?
?
--end?
?
?