JAVA得到IP地址信息

10年前

/*

     * 得到IP地址集合

     */

    public ArrayList<String> getIps(){

       ArrayList<String> ips = new ArrayList<String>();

       InetAddress inetAddress;

       try {

           Enumeration<NetworkInterface> ifaces =

               NetworkInterface.getNetworkInterfaces();

           while (ifaces.hasMoreElements()) {

               NetworkInterface iface = ifaces.nextElement();

               Enumeration<InetAddress> addrs = iface.getInetAddresses();

               while (addrs.hasMoreElements()) {

                    inetAddress = addrs.nextElement();

                   

                    if (inetAddress instanceof Inet4Address){

                       ips.add(inetAddress.getHostAddress());

//                    NetworkInterface ni = NetworkInterface.getByInetAddress(inetAddress);

//                    if(ni.getHardwareAddress() != null){

//                       

//                        System.out.println(ni.getDisplayName());

//                    }

                    }

               }

           }

       } catch (SocketException e) {

           e.printStackTrace();

       }

       return ips;

    }

   

 

如果想得到网卡其他信息,请参考API