mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix HostInfo
This commit is contained in:
parent
fb488a0081
commit
8db785ea89
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【http】 修复HttpRquest中body方法长度计算问题(issue#I10UPG@Gitee)
|
* 【http】 修复HttpRquest中body方法长度计算问题(issue#I10UPG@Gitee)
|
||||||
|
* 【system】 修复获取本地IP问题(pr#65@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
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 cn.hutool.core.net.NetUtil;
|
||||||
import java.util.Enumeration;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代表当前主机的信息。
|
* 代表当前主机的信息。
|
||||||
@ -15,28 +13,11 @@ public class HostInfo implements Serializable {
|
|||||||
|
|
||||||
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;
|
final InetAddress localhost = NetUtil.getLocalhost();
|
||||||
String hostAddress;
|
HOST_NAME = localhost.getHostName();
|
||||||
String hostNetworkCardAddress;
|
HOST_ADDRESS = localhost.getHostAddress();
|
||||||
|
|
||||||
try {
|
|
||||||
InetAddress localhost = InetAddress.getLocalHost();
|
|
||||||
|
|
||||||
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_NETWORK_CARD_ADDRESS = hostNetworkCardAddress;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -65,45 +46,6 @@ public class HostInfo implements Serializable {
|
|||||||
return HOST_ADDRESS;
|
return HOST_ADDRESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 取得当前主机的网卡IP地址。
|
|
||||||
* <p>
|
|
||||||
* 例如:<code>"192.168.0.1"</code>
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @return 网卡IP地址
|
|
||||||
*/
|
|
||||||
public final String getNetworkCardAddress() {
|
|
||||||
return HOST_NETWORK_CARD_ADDRESS;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获得网卡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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将当前主机的信息转换成字符串。
|
* 将当前主机的信息转换成字符串。
|
||||||
*
|
*
|
||||||
@ -115,7 +57,6 @@ public class HostInfo implements Serializable {
|
|||||||
|
|
||||||
SystemUtil.append(builder, "Host Name: ", getName());
|
SystemUtil.append(builder, "Host Name: ", getName());
|
||||||
SystemUtil.append(builder, "Host Address: ", getAddress());
|
SystemUtil.append(builder, "Host Address: ", getAddress());
|
||||||
SystemUtil.append(builder, "Host NETWORK CARD ADDRESS: ", getNetworkCardAddress());
|
|
||||||
|
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user