Java获取本地IP地址

原创
04/08 23:01
阅读数 133
//获取本地IP
public static void getLocalIntranetIp() {
        try {
            Enumeration<NetworkInterface> InterfaceList = NetworkInterface.getNetworkInterfaces();
            while (InterfaceList.hasMoreElements()) {
                NetworkInterface iFace = InterfaceList.nextElement();
                if (iFace.isLoopback() || iFace.isVirtual() || iFace.isPointToPoint() || !iFace.isUp()) continue;
                Enumeration<InetAddress> AddrList = iFace.getInetAddresses();
                while (AddrList.hasMoreElements()) {
                    InetAddress address = AddrList.nextElement();
                    if ((address instanceof Inet4Address)) {// 只关心IPv4地址,不关心IPV6 address instanceof Inet6Address
                        System.out.println("--------ip--------" + address.getHostAddress());
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部