码上敲享录 > java入门知识分享 > java获取服务的局域网ip

java获取服务的局域网ip

上一章章节目录下一章 2021-07-23已有762人阅读 评论(0)

java获取服务的局域网ip


解决方法:

private static String getLocalHostLANAddress() {

       try {

           InetAddress candidateAddress = null;

           Enumeration ifaces = NetworkInterface.getNetworkInterfaces();


           while(ifaces.hasMoreElements()) {

               NetworkInterface iface = (NetworkInterface)ifaces.nextElement();

               Enumeration inetAddrs = iface.getInetAddresses();


               while(inetAddrs.hasMoreElements()) {

                   InetAddress inetAddr = (InetAddress)inetAddrs.nextElement();

                   if (!inetAddr.isLoopbackAddress()) {

                       if (inetAddr.isSiteLocalAddress()) {

                           return inetAddr;

                       }


                       if (candidateAddress == null) {

                           candidateAddress = inetAddr;

                       }

                   }

               }

           }


           if (candidateAddress != null) {

               return candidateAddress;

           } else {

               InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();

               return jdkSuppliedAddress.getHostName();

           }

       } catch (Exception var5) {

           return null;

       }

   }


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交