用Java获取本地的多个IP地址

翻API翻出来的,拿出来晒晒。。。
特别适用于获取本地的多个IP地址

 

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class GetIP {
    public static void main(String[] args) throws UnknownHostException,
            SocketException {
        System.out.println("Host:/t" + InetAddress.getLocalHost() + "/n");
        Enumeration en = NetworkInterface
                .getNetworkInterfaces();
        Enumeration addresses;
        while (en.hasMoreElements()) {
            NetworkInterface networkinterface = en.nextElement();
            System.out.println(networkinterface.getName());
            addresses = networkinterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
                System.out.println("/t"
                        + addresses.nextElement().getHostAddress() + "");
            }
        }
    }
}

你可能感兴趣的:(Java)