相关疑难解决方法(0)

检查ipAddress是否在私有范围内

我如何检查IP地址是否属于私有类别?

if(isPrivateIPAddress(ipAddress)) {
    //do something
}
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激.

更新的答案

private static boolean isPrivateIPAddress(String ipAddress) {

        InetAddress ia = null;

        try {
            InetAddress ad = InetAddress.getByName(ipAddress);
            byte[] ip = ad.getAddress();
            ia = InetAddress.getByAddress(ip);
        } catch (UnknownHostException e) {

            e.printStackTrace();
        }

        return ia.isSiteLocalAddress();
    }
Run Code Online (Sandbox Code Playgroud)

我写了这个方法,它对我来说很好.但有没有这种方法不起作用的情况?我只是想确保它适用于每个案例.

java

11
推荐指数
3
解决办法
1万
查看次数

标签 统计

java ×1