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

来源:互联网 发布:uml建模软件 编辑:程序博客网 时间:2024/04/30 19:54

翻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<NetworkInterface> en = NetworkInterface
                .getNetworkInterfaces();
        Enumeration<InetAddress> 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() + "");
            }
        }
    }
}

原创粉丝点击