日期:2014-05-17  浏览次数:20774 次

Windows系统下Java获得获得多网卡ip
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/*********************************
可以获得多网卡IP
只适合在Windows 下使用
 *********************************/
public class GetMoreIp {
    public static void main1(String[] args) {
        Enumeration netInterfaces = null;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();

            while (netInterfaces.hasMoreElements()) {

                NetworkInterface ni = netInterfaces.nextElement();
                System.out.println("DisplayName:" + ni.getDisplayName());
                System.out.println("Name:" + ni.getName());

                Enumeration ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    System.out.println("IP:"
                            + ips.nextElement().getHostAddress());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Enumeration netInterfaces = null;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();

            while (netInterfaces.hasMoreElements()) {

                NetworkInterface ni = (NetworkInterface) netInterfaces
                        .nextElement();
                System.out.println("DisplayName:" + ni.getDisplayName());
                System.out.println("Name:" + ni.getName());
                Enumeration ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    System.out.println("IP:"+ ((InetAddress) ips.nextElement()).getHostAddress());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}