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 閱讀(4362) 評論(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 哈哈哈
          必回
          主站蜘蛛池模板: 江源县| 客服| 通山县| 吉木萨尔县| 定州市| 揭东县| 海丰县| 色达县| 湘阴县| 宜都市| 金溪县| 尤溪县| 霍林郭勒市| 东光县| 黄骅市| 柳州市| 买车| 南澳县| 盐山县| 成都市| 民和| 宜昌市| 红桥区| 宜兴市| 镇远县| 洛南县| 霍州市| 双城市| 马鞍山市| 普宁市| 曲靖市| 丹棱县| 墨脱县| 泾阳县| 永德县| 郑州市| 大丰市| 德令哈市| 清苑县| 通江县| 浙江省|