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 閱讀(4370) 評論(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 哈哈哈
          必回
          主站蜘蛛池模板: 延吉市| 崇义县| 吕梁市| 旺苍县| 龙海市| 常熟市| 措美县| 新绛县| 阳谷县| 湖南省| 天台县| 临海市| 南康市| 固始县| 屏山县| 密云县| 商都县| 盘锦市| 河东区| 汾阳市| 广宗县| 贵南县| 聂拉木县| 象州县| 万载县| 晋宁县| 泰安市| 桐乡市| 昭觉县| 平武县| 方山县| 青岛市| 临夏县| 东至县| 潜江市| 康马县| 陕西省| 论坛| 灵石县| 庐江县| 阳山县|