获取本机的IPv6地址

废话少说,直接上代码(java)
命令行输出的就是本机的IPv6地址

package app;

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

public class IPv6 {
    public static void main(String[] args) {
        /*获取本机所有ip地址(包括保留地址,ipv4,ipv6  如果安装了虚拟机会更多其他的地址)
         * try {
            InetAddress ads = null;
            Enumeration   adds = NetworkInterface.getNetworkInterfaces();
            while(adds.hasMoreElements()) {
            Enumeration inetAds = adds.nextElement().getInetAddresses();
                while(inetAds.hasMoreElements()) {
                    ads = inetAds.nextElement();
                    System.out.println(ads.getHostAddress());
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        
        //获取可用ipv6地址
        try {
            System.out.println(getLocalIPv6Address());
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static String getLocalIPv6Address() throws SocketException {
        InetAddress inetAddress =null;
        
        Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();
        outer:
            while(networkInterfaces.hasMoreElements()) {
                Enumeration inetAds = networkInterfaces.nextElement().getInetAddresses();
                while(inetAds.hasMoreElements()) {
                    inetAddress = inetAds.nextElement();
                    //检查此地址是否是IPv6地址以及是否是保留地址
                    if(inetAddress instanceof Inet6Address&& !isReservedAddr(inetAddress)) {
                        break outer;
                        
                    }
                }
            }
        String ipAddr = inetAddress.getHostAddress();
        //过滤网卡
        int index = ipAddr.indexOf('%');
        if(index>0) {
            ipAddr = ipAddr.substring(0, index);
        }
        
        return ipAddr;
    }
    private static boolean isReservedAddr(InetAddress inetAddr) {
        if(inetAddr.isAnyLocalAddress()||inetAddr.isLinkLocalAddress()||inetAddr.isLoopbackAddress())
        {
            return true;
        }
        return false;
    }
}

可以使用命令行进行对比
如果是window 输入 ipconfig进行查询
如果是 Linux 输入 ifconfig 进行查询

你可能感兴趣的:(获取本机的IPv6地址)