java.net.inetaddress_基于java.net.InetAddress定义AddressUtils工具类实现校验有效端口、本地IP地址及获取本地InetAddress地址等相关操作...

一、前言

通过java.net.InetAddress类定义网络地址工具类,实现本地端口有效验证(isAvailablePort)、域名地址有效验证(isValidHostAddress)、获取域名ip/hostName(getHostIp/getHostName)、获取本地InetAddress对象(getHostAddress)等操作。

二、代码示例import java.io.IOException;@b@import java.net.InetAddress;@b@import java.net.NetworkInterface;@b@import java.net.ServerSocket;@b@import java.util.Enumeration;@b@import java.util.regex.Pattern; @b@@b@public class AddressUtils {@b@ @b@    private static final String  LOCALHOST_IP = "127.0.0.1";@b@    private static final String  EMPTY_IP     = "0.0.0.0";@b@    private static final Pattern IP_PATTERN   = Pattern.compile("[0-9]{1,3}(\\.[0-9]{1,3}){3,}");@b@@b@    public static boolean isAvailablePort(int port) {@b@        ServerSocket ss = null;@b@        try {@b@            ss = new ServerSocket(port);@b@            ss.bind(null);@b@            return true;@b@        } catch (IOException e) {@b@            return false;@b@        } finally {@b@            if (ss != null) {@b@                try {@b@                    ss.close();@b@                } catch (IOException e) {@b@                }@b@            }@b@        }@b@    }@b@@b@    private static boolean isValidHostAddress(InetAddress address) {@b@        if (address == null || address.isLoopbackAddress()) {@b@            return false;@b@        }@b@        String name = address.getHostAddress();@b@        return (name != null && !EMPTY_IP.equals(name) && !LOCALHOST_IP.equals(name) && IP_PATTERN.matcher(name)@b@            .matches());@b@    }@b@@b@    public static String getHostIp() {@b@        InetAddress address = getHostAddress();@b@        return address == null ? null : address.getHostAddress();@b@    }@b@@b@    public static String getHostName() {@b@        InetAddress address = getHostAddress();@b@        return address == null ? null : address.getHostName();@b@    }@b@@b@    public static InetAddress getHostAddress() {@b@        InetAddress localAddress = null;@b@        try {@b@            localAddress = InetAddress.getLocalHost();@b@            if (isValidHostAddress(localAddress)) {@b@                return localAddress;@b@            }@b@        } catch (Throwable e) {@b@            system.out.println("Failed to retriving local host ip address, try scan network card ip address. cause: "@b@                        + e.getMessage());@b@        }@b@        try {@b@            Enumeration interfaces = NetworkInterface.getNetworkInterfaces();@b@            if (interfaces != null) {@b@                while (interfaces.hasMoreElements()) {@b@                    try {@b@                        NetworkInterface network = interfaces.nextElement();@b@                        Enumeration addresses = network.getInetAddresses();@b@                        if (addresses != null) {@b@                            while (addresses.hasMoreElements()) {@b@                                try {@b@                                    InetAddress address = addresses.nextElement();@b@                                    if (isValidHostAddress(address)) {@b@                                        return address;@b@                                    }@b@                                } catch (Throwable e) {@b@                                    system.out.println("Failed to retriving network card ip address. cause:" + e.getMessage());@b@                                }@b@                            }@b@                        }@b@                    } catch (Throwable e) {@b@                        system.out.println("Failed to retriving network card ip address. cause:" + e.getMessage());@b@                    }@b@                }@b@            }@b@        } catch (Throwable e) {@b@            system.out.println("Failed to retriving network card ip address. cause:" + e.getMessage());@b@        }@b@        system.out.println("Could not get local host ip address, will use 127.0.0.1 instead.");@b@        return localAddress;@b@    }@b@}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值