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 閱讀(4372) 評論(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 哈哈哈
          必回
          主站蜘蛛池模板: 康定县| 衡东县| 金寨县| 崇义县| 集安市| 辽宁省| 寻甸| 河曲县| 宜宾市| 纳雍县| 隆林| 辛集市| 扎兰屯市| 子长县| 澄江县| 墨竹工卡县| 集安市| 古交市| 社会| 柏乡县| 泗洪县| 遵义市| 济宁市| 广东省| 蒙阴县| 庄河市| 城口县| 通州区| 岐山县| 和林格尔县| 墨江| 五河县| 太谷县| 兖州市| 宁夏| 晋州市| 潢川县| 衡山县| 武汉市| 安化县| 射洪县|