From fef0e9a358180f2e32f9370f9f0ced572b3b457b Mon Sep 17 00:00:00 2001 From: lxd <79963354@qq.com> Date: Tue, 20 Aug 2019 16:34:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E7=BD=91?= =?UTF-8?q?=E5=8D=A1IP=E5=9C=B0=E5=9D=80=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/system/HostInfo.java | 156 ++++++++++++------ 1 file changed, 102 insertions(+), 54 deletions(-) diff --git a/hutool-system/src/main/java/cn/hutool/system/HostInfo.java b/hutool-system/src/main/java/cn/hutool/system/HostInfo.java index 34c08f5c1..0880c876e 100644 --- a/hutool-system/src/main/java/cn/hutool/system/HostInfo.java +++ b/hutool-system/src/main/java/cn/hutool/system/HostInfo.java @@ -1,75 +1,123 @@ package cn.hutool.system; import java.io.Serializable; +import java.net.Inet4Address; import java.net.InetAddress; +import java.net.NetworkInterface; import java.net.UnknownHostException; +import java.util.Enumeration; /** * 代表当前主机的信息。 */ -public class HostInfo implements Serializable{ - private static final long serialVersionUID = 1L; +public class HostInfo implements Serializable { + private static final long serialVersionUID = 1L; - private final String HOST_NAME; - private final String HOST_ADDRESS; + private final String HOST_NAME; + private final String HOST_ADDRESS; + private final String HOST_NETWORK_CARD_ADDRESS; - public HostInfo() { - String hostName; - String hostAddress; + public HostInfo() { + String hostName; + String hostAddress; + String hostNetworkCardAddress; - try { - InetAddress localhost = InetAddress.getLocalHost(); + try { + InetAddress localhost = InetAddress.getLocalHost(); - hostName = localhost.getHostName(); - hostAddress = localhost.getHostAddress(); - } catch (UnknownHostException e) { - hostName = "localhost"; - hostAddress = "127.0.0.1"; - } + hostName = localhost.getHostName(); + hostAddress = localhost.getHostAddress(); + hostNetworkCardAddress = getInternetIp(); + } catch (UnknownHostException e) { + hostName = "localhost"; + hostAddress = "127.0.0.1"; + hostNetworkCardAddress = "127.0.0.1"; + } - HOST_NAME = hostName; - HOST_ADDRESS = hostAddress; - } + HOST_NAME = hostName; + HOST_ADDRESS = hostAddress; + HOST_NETWORK_CARD_ADDRESS = hostNetworkCardAddress; + } - /** - * 取得当前主机的名称。 - * - *

- * 例如:"webserver1" - *

- * - * @return 主机名 - */ - public final String getName() { - return HOST_NAME; - } + /** + * 取得当前主机的名称。 + * + *

+ * 例如:"webserver1" + *

+ * + * @return 主机名 + */ + public final String getName() { + return HOST_NAME; + } - /** - * 取得当前主机的地址。 - * - *

- * 例如:"192.168.0.1" - *

- * - * @return 主机地址 - */ - public final String getAddress() { - return HOST_ADDRESS; - } + /** + * 取得当前主机的地址。 + * + *

+ * 例如:"192.168.0.1" + *

+ * + * @return 主机地址 + */ + public final String getAddress() { + return HOST_ADDRESS; + } - /** - * 将当前主机的信息转换成字符串。 - * - * @return 主机信息的字符串表示 - */ - @Override - public final String toString() { - StringBuilder builder = new StringBuilder(); + /** + * 取得当前主机的网卡IP地址。 + *

+ * 例如:"192.168.0.1" + *

+ * + * @return 网卡IP地址 + */ + 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 networks = NetworkInterface.getNetworkInterfaces(); + InetAddress ip = null; + Enumeration 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(); + } }