diff --git a/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java b/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java index 2d47fc7..c6b2cb4 100644 --- a/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java +++ b/huaheng-wms-core/src/main/java/org/jeecg/JeecgSystemApplication.java @@ -1,10 +1,15 @@ package org.jeecg; +import java.net.Inet4Address; +import java.net.Inet6Address; import java.net.InetAddress; import java.net.NetworkInterface; +import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; +import javax.transaction.SystemException; + import org.jeecg.common.util.oConvertUtils; import org.springframework.boot.SpringApplication; //import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -32,7 +37,7 @@ public class JeecgSystemApplication extends SpringBootServletInitializer { return application.sources(JeecgSystemApplication.class); } - public static void main(String[] args) throws UnknownHostException { + public static void main(String[] args) throws SystemException, UnknownHostException { ConfigurableApplicationContext application = SpringApplication.run(JeecgSystemApplication.class, args); Environment env = application.getEnvironment(); String ip = getLocalHostExactAddress().getHostAddress(); @@ -46,40 +51,29 @@ public class JeecgSystemApplication extends SpringBootServletInitializer { + "WEB External: \thttp://" + ip + ":" + port + path + "/index.html\n\t" + "API External: \thttp://" + ip + ":" + port + path + "/\n\n\t" + "Swagger文档: \thttp://" + ip + ":" + port + path + "/doc.html\n\n\t" - + "The following profiles are active: " + profiles + "\n" + + "The following profiles are active: [" + profiles + "]\n" + "----------------------------------------------------------"); } - + public static InetAddress getLocalHostExactAddress() { try { - InetAddress candidateAddress = null; + InetAddress inetAddress = null; Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { - NetworkInterface iface = networkInterfaces.nextElement(); - // 该网卡接口下的ip会有多个,也需要一个个的遍历,找到自己所需要的 - for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) { - InetAddress inetAddr = inetAddrs.nextElement(); - // 排除loopback回环类型地址(不管是IPv4还是IPv6 只要是回环地址都会返回true) - if (!inetAddr.isLoopbackAddress()) { - if (inetAddr.isSiteLocalAddress()) { - // 如果是site-local地址,就是它了 就是我们要找的 - // ~~~~~~~~~~~~~绝大部分情况下都会在此处返回你的ip地址值~~~~~~~~~~~~~ - return inetAddr; - } - - // 若不是site-local地址 那就记录下该地址当作候选 - if (candidateAddress == null) { - candidateAddress = inetAddr; - } - + NetworkInterface netInterface = networkInterfaces.nextElement(); + // System.out.println(netInterface.getName()); + Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); + while (addresses.hasMoreElements()) { + InetAddress ipTmp = addresses.nextElement(); + if (ipTmp != null && ipTmp instanceof Inet4Address && ipTmp.isSiteLocalAddress() && !ipTmp.isLoopbackAddress() + && ipTmp.getHostAddress().indexOf(":") == -1) { + inetAddress = ipTmp; } } } - - // 如果出去loopback回环地之外无其它地址了,那就回退到原始方案吧 - return candidateAddress == null ? InetAddress.getLocalHost() : candidateAddress; - } catch (Exception e) { + return inetAddress == null ? InetAddress.getLocalHost() : inetAddress; + } catch (SocketException | UnknownHostException e) { e.printStackTrace(); } return null;