首页  |  知识库  |  资源下载  |  在线工具  |  A-Z  •  JAR  •  名词查         

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

标签:InetAddress,AddressUtils,网址工具类,端口校验,获取ip,区域地址     发布时间:2018-08-09   

一、前言

通过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<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();@b@            if (interfaces != null) {@b@                while (interfaces.hasMoreElements()) {@b@                    try {@b@                        NetworkInterface network = interfaces.nextElement();@b@                        Enumeration<InetAddress> 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@}
  • ◆ 相关内容