!65 新增获取网卡IP地址的方法

Merge pull request !65 from 无畏/v4-dev
This commit is contained in:
Looly 2019-08-20 17:50:33 +08:00 committed by Gitee
commit fb488a0081
2 changed files with 108 additions and 54 deletions

View File

@ -1,75 +1,123 @@
package cn.hutool.system; package cn.hutool.system;
import java.io.Serializable; import java.io.Serializable;
import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Enumeration;
/** /**
* 代表当前主机的信息 * 代表当前主机的信息
*/ */
public class HostInfo implements Serializable{ public class HostInfo implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final String HOST_NAME; private final String HOST_NAME;
private final String HOST_ADDRESS; private final String HOST_ADDRESS;
private final String HOST_NETWORK_CARD_ADDRESS;
public HostInfo() { public HostInfo() {
String hostName; String hostName;
String hostAddress; String hostAddress;
String hostNetworkCardAddress;
try { try {
InetAddress localhost = InetAddress.getLocalHost(); InetAddress localhost = InetAddress.getLocalHost();
hostName = localhost.getHostName(); hostName = localhost.getHostName();
hostAddress = localhost.getHostAddress(); hostAddress = localhost.getHostAddress();
} catch (UnknownHostException e) { hostNetworkCardAddress = getInternetIp();
hostName = "localhost"; } catch (UnknownHostException e) {
hostAddress = "127.0.0.1"; hostName = "localhost";
} hostAddress = "127.0.0.1";
hostNetworkCardAddress = "127.0.0.1";
}
HOST_NAME = hostName; HOST_NAME = hostName;
HOST_ADDRESS = hostAddress; HOST_ADDRESS = hostAddress;
} HOST_NETWORK_CARD_ADDRESS = hostNetworkCardAddress;
}
/** /**
* 取得当前主机的名称 * 取得当前主机的名称
* *
* <p> * <p>
* 例如<code>"webserver1"</code> * 例如<code>"webserver1"</code>
* </p> * </p>
* *
* @return 主机名 * @return 主机名
*/ */
public final String getName() { public final String getName() {
return HOST_NAME; return HOST_NAME;
} }
/** /**
* 取得当前主机的地址 * 取得当前主机的地址
* *
* <p> * <p>
* 例如<code>"192.168.0.1"</code> * 例如<code>"192.168.0.1"</code>
* </p> * </p>
* *
* @return 主机地址 * @return 主机地址
*/ */
public final String getAddress() { public final String getAddress() {
return HOST_ADDRESS; return HOST_ADDRESS;
} }
/** /**
* 将当前主机的信息转换成字符串 * 取得当前主机的网卡IP地址
* * <p>
* @return 主机信息的字符串表示 * 例如<code>"192.168.0.1"</code>
*/ * </p>
@Override *
public final String toString() { * @return 网卡IP地址
StringBuilder builder = new StringBuilder(); */
public final String getNetworkCardAddress() {
return HOST_NETWORK_CARD_ADDRESS;
}
SystemUtil.append(builder, "Host Name: ", getName()); /**
SystemUtil.append(builder, "Host Address: ", getAddress()); * 获得网卡IP
*
* @return 网卡IP
*/
public static String getInternetIp() {
String INTRANET_IP = null;
try {
Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
Enumeration<InetAddress> addrs;
while (networks.hasMoreElements()) {
addrs = networks.nextElement().getInetAddresses();
while (addrs.hasMoreElements()) {
ip = addrs.nextElement();
if (ip != null && ip instanceof Inet4Address && ip.isSiteLocalAddress() && !ip.getHostAddress().equals(INTRANET_IP)) {
return ip.getHostAddress();
}
}
}
// 如果没有网卡IP就返回/etc/hosts IP
return INTRANET_IP;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return builder.toString(); /**
} * 将当前主机的信息转换成字符串
*
* @return 主机信息的字符串表示
*/
@Override
public final String toString() {
StringBuilder builder = new StringBuilder();
SystemUtil.append(builder, "Host Name: ", getName());
SystemUtil.append(builder, "Host Address: ", getAddress());
SystemUtil.append(builder, "Host NETWORK CARD ADDRESS: ", getNetworkCardAddress());
return builder.toString();
}
} }

View File

@ -29,5 +29,11 @@ public class SystemUtilTest {
OsInfo osInfo = SystemUtil.getOsInfo(); OsInfo osInfo = SystemUtil.getOsInfo();
Assert.assertNotNull(osInfo); Assert.assertNotNull(osInfo);
} }
@Test
public void getHostInfo() {
HostInfo hostInfo = SystemUtil.getHostInfo();
Assert.assertNotNull(hostInfo);
}
} }