mirror of
https://gitee.com/wangkang/udf.git
synced 2026-11-02 22:53:43 +00:00
更新
This commit is contained in:
+16
-11
@@ -467,22 +467,27 @@ public class CoreUtil {
|
||||
* @throws UnknownHostException
|
||||
*/
|
||||
public static InetAddress getLocalHostLANAddress() throws UnknownHostException {
|
||||
//排除docker
|
||||
final String excludeDocker = "docker";
|
||||
//处理
|
||||
try {
|
||||
InetAddress candidateAddress = null;
|
||||
// 遍历所有的网络接口
|
||||
for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) {
|
||||
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
|
||||
log.info("{} , {}", iface.getName(), iface.getDisplayName());
|
||||
// 在所有的接口下再遍历IP
|
||||
for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
|
||||
InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
|
||||
if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
|
||||
if (inetAddr.isSiteLocalAddress()) {
|
||||
// 如果是site-local地址,就是它了
|
||||
return inetAddr;
|
||||
} else if (candidateAddress == null) {
|
||||
// site-local类型的地址未被发现,先记录候选地址
|
||||
candidateAddress = inetAddr;
|
||||
//排除docker
|
||||
if (!iface.getName().contains(excludeDocker)) {
|
||||
// 在所有的接口下再遍历IP
|
||||
for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
|
||||
InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
|
||||
if (!inetAddr.isLoopbackAddress()) {// 排除loopback类型地址
|
||||
if (inetAddr.isSiteLocalAddress()) {
|
||||
// 如果是site-local地址,就是它了
|
||||
return inetAddr;
|
||||
} else if (candidateAddress == null) {
|
||||
// site-local类型的地址未被发现,先记录候选地址
|
||||
candidateAddress = inetAddr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user