下面的代码中我做了校验真实的ipv6获取ipv6去除%后面的值然后通过冒号校验位数,然后截取第一段判断是否包括fe或者fc包括就表示假的

 //ipv6    public static String getLocalIpV6() {        try {            for (Enumeration en = NetworkInterface                    .getNetworkInterfaces(); en.hasMoreElements(); ) {                NetworkInterface intf = en.nextElement();                for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {                    InetAddress inetAddress = enumIpAddr.nextElement();                    // logger.error("ip1       " + inetAddress);                    logger.error("ip1  " + inetAddress.getHostAddress());                 /*   logger.error("getHostName  " + inetAddress.getHostName());                    logger.error("getCanonicalHostName  " + inetAddress.getCanonicalHostName());                    logger.error("getAddress  " + Arrays.toString(inetAddress.getAddress()));                    logger.error("getHostAddress  " + inetAddress.getHostAddress());*/                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {                        return inetAddress.getHostAddress();                    }                }            }        } catch (Exception ex) {            Log.e("IP Address", ex.toString());        }        return null;    }    public static String getlocalIp() {        String ip;        try {            for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {                NetworkInterface intf = en.nextElement();                for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {                    InetAddress inetAddress = enumIpAddr.nextElement();                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {//                           ip=inetAddress.getHostAddress().toString();                        System.out.println("ip==========" + inetAddress.getHostAddress());                        return inetAddress.getHostAddress();                    }                }            }        } catch (SocketException ignored) {        }        return null;    }    public static String validateV6() {              new Thread(new Runnable() {            @Override            public void run() {                hostIp6 = getLocalIpV6();            }        }).start();        //过滤找到真实的ipv6地址        logger.error("v6 validateV6 " + hostIp6);        if (hostIp6 != null && hostIp6.contains("%")) {            String[] split = hostIp6.split("%");            String s1 = split[0];            logger.error("v6 remove % is " + s1);            if (s1 != null && s1.contains(":")) {                String[] split1 = s1.split(":");                if (split1.length == 6||split1.length==8) {                    if (split1[0].contains("fe") || split1[0].contains("fc")) {                        return "0.0.0.0";                    } else {                        return s1;                    }                }            }        }        return "0.0.0.0";    }

欢迎加入技术分享学习公众号【码兄】

更多相关文章

  1. Android(安卓)string.xml 显示特殊符号
  2. Android框架之路——ToolBar的使用
  3. Android---配置adb环境变量
  4. Android微信分享---点击分享伤害了我且一闪而过~
  5. Vue实现掘金导航栏效果
  6. Vue实现简书导航栏效果
  7. android号码匹配位的修改
  8. android在代码里设置button或者textView的字体颜色
  9. 2017年11月1日Android职位数据分析

随机推荐

  1. Intent.ACTION_TIME_TICK的正确用法
  2. 在Android(安卓)5.0中使用JobScheduler
  3. Android(安卓)Storage Manager
  4. Android引路蜂地图开发示例:地址反编码
  5. Android(安卓)Studio中AIDL使用方法
  6. Android进程通信之Messenger&AIDL使用详
  7. Android实现BMP和PNG转换为JPEG格式
  8. android百分比布局PercentRelativeLayout
  9. android中修改string.xml中变量
  10. Android(安卓)build system 分析之 envse