需求: html
基於url獲取host,case以下: url
http://snv.iteye.com/blog/1992991結果爲snv.iteye.com spa
snv.iteye.com/blog/1992991結果爲snv.iteye.com htm
https://snv.iteye.com/blog/1992991結果爲snv.iteye.com blog
http://snv.iteye.html結果爲「」 get
http://snv.iteye.htm結果爲「」 it
snv.iteye.html結果爲「」 io
teye.html結果爲「」 方法
http://www.iteye.com/blog/1992991結果爲www.iteye.com im
www.iteye.com/blog/1992991結果爲www.iteye.com
https://www.iteye.com/blog/1992991結果爲www.iteye.com
方法實現以下:
方法1:基於URI或者URL
依賴:
Xml代碼
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
代碼:
Java代碼
- 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;
- }
方法2:基於正則
依賴:
Xml代碼
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- <version>2.6</version>
- </dependency>
代碼:
Java代碼
- 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;
- }