---
title: "Java基于url获取host的两种方法"
author: "小网客"
date: "2013-12-26T10:02:10Z"
tags: []
categories: ["编程语言"]
slug: "java-url-host"
draft: false
lastmod: "2013-12-26T10:02:10Z"
id: 1995604
url: "https://snv.iteye.com/blog/1995604"
---

需求：

基于url获取host，case如下：

[http://snv.iteye.com/blog/1992991](/blog/1992991)结果为snv.iteye.com

[snv.iteye.com/blog/1992991](/blog/1992991)结果为snv.iteye.com

[https://snv.iteye.com/blog/1992991](/blog/1992991)结果为snv.iteye.com

[http://snv.iteye.html](/blog/1992991)结果为“”

[http://snv.iteye.htm](/blog/1992991)结果为“”

[snv.iteye.html](/blog/1992991)结果为“”

[teye.html](/blog/1992991)结果为“”

[http://www.iteye.com/blog/1992991](/blog/1992991)结果为www.iteye.com

www[.iteye.com/blog/1992991](/blog/1992991)结果为www.iteye.com

[https://www.iteye.com/blog/1992991](/blog/1992991)结果为www.iteye.com

 

方法实现如下：

方法1：基于URI或者URL

依赖：

```
<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>2.6</version>
</dependency>
```

代码：

```
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：基于正则

依赖：

```
<dependency>
	<groupId>commons-lang</groupId>
	<artifactId>commons-lang</artifactId>
	<version>2.6</version>
</dependency>
```

代码：

```
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;
}
```