ice world

          There is nothing too difficult if you put your heart into it.
          posts - 104, comments - 103, trackbacks - 0, articles - 0

          Java獲取本機IP列表

          Posted on 2013-07-19 11:10 IceWee 閱讀(4363) 評論(1)  編輯  收藏 所屬分類: Java
          Java獲取本地IP地址方法網上搜一籮筐,但基本上都是獲得一個IP,實際開發中一臺電腦很可能有多個IP地址,如多網卡,或者安裝了VM Ware虛擬機,就會虛擬出其他的網卡,那么傳統的方法得到的一個IP地址就不全面了,下面貼出獲取本機IP列表的方法:
          /**
               * IceWee 2013.07.19
               * 獲取本地IP列表(針對多網卡情況)
               *
               * 
          @return
               
          */

              
          public static List<String> getLocalIPList() {
                  List
          <String> ipList = new ArrayList<String>();
                  
          try {
                      Enumeration
          <NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                      NetworkInterface networkInterface;
                      Enumeration
          <InetAddress> inetAddresses;
                      InetAddress inetAddress;
                      String ip;
                      
          while (networkInterfaces.hasMoreElements()) {
                          networkInterface 
          = networkInterfaces.nextElement();
                          inetAddresses 
          = networkInterface.getInetAddresses();
                          
          while (inetAddresses.hasMoreElements()) {
                              inetAddress 
          = inetAddresses.nextElement();
                              
          if (inetAddress != null && inetAddress instanceof Inet4Address) // IPV4
                                  ip = inetAddress.getHostAddress();
                                  ipList.add(ip);
                              }

                          }

                      }

                  }
           catch (SocketException e) {
                      e.printStackTrace();
                  }

                  
          return ipList;
              }




          Feedback

          # re: Java獲取本機IP列表  回復  更多評論   

          2016-03-14 14:38 by 哈哈哈
          必回
          主站蜘蛛池模板: 清水河县| 九龙坡区| 绥芬河市| 灵台县| 南澳县| 兴义市| 嘉义市| 拉萨市| 江城| 健康| 东海县| 柳河县| 双江| 岗巴县| 涟源市| 军事| 永川市| 磐石市| 龙州县| 睢宁县| 西充县| 通辽市| 雅江县| 丹东市| 犍为县| 石嘴山市| 济南市| 仪陇县| 河西区| 高淳县| 四川省| 治县。| 武定县| 江川县| 会宁县| 商南县| 雷波县| 乐业县| 资源县| 清流县| 阳原县|