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 閱讀(4371) 評論(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 哈哈哈
          必回
          主站蜘蛛池模板: 静乐县| 汉沽区| 白玉县| 安达市| 浮山县| 丰宁| 铅山县| 砚山县| 桐梓县| 三都| 木兰县| 东山县| 马尔康县| 镇原县| 盐源县| 开平市| 波密县| 田阳县| 东莞市| 咸宁市| 吉安市| 安远县| 孝感市| 达孜县| 禹城市| 南靖县| 礼泉县| 张家港市| 乌拉特中旗| 青岛市| 福海县| 泸西县| 莆田市| 洛浦县| 慈溪市| 遂川县| 绵阳市| 林芝县| 佛坪县| 温泉县| 常熟市|