public class NetUtil extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
LOCAL_IP
本地IP
|
static int |
PORT_RANGE_MAX
默认最大端口,65535
|
static int |
PORT_RANGE_MIN
默认最小端口,1024
|
| Constructor and Description |
|---|
NetUtil() |
| Modifier and Type | Method and Description |
|---|---|
static InetSocketAddress |
createAddress(String host,
int port)
|
static String |
getAddressName(InetAddress address)
获取地址名称,如果无名称返回地址
如果提供的地址为 null返回null |
static List<String> |
getDnsInfo(String hostName,
String... attrNames)
获取DNS信息,如TXT信息:
NetUtil.attrNames("hutool.cn", "TXT")
|
static String |
getIpByHost(String hostName)
通过域名得到IP
|
static String |
getLocalhostStrV4()
获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个
如果获取失败调用 InetAddress.getLocalHost()方法获取。 |
static InetAddress |
getLocalhostV4()
获取本机网卡IPv4地址,规则如下:
必须非回路(loopback)地址、非局域网地址(siteLocal)、IPv4地址
多网卡则返回第一个满足条件的地址
如果无满足要求的地址,调用
InetAddress.getLocalHost() 获取地址
此方法不会抛出异常,获取失败将返回null见:https://github.com/dromara/hutool/issues/428 |
static String |
getLocalMacAddressV4()
获得本机MAC地址,默认使用获取到的IPv4本地地址对应网卡
|
static String |
getMultistageReverseProxyIp(String ip)
从多级反向代理中获得第一个非unknown IP地址
|
static NetworkInterface |
getNetworkInterface(String name)
获取指定名称的网卡信息
|
static Collection<NetworkInterface> |
getNetworkInterfaces()
获取本机所有网卡
|
static int |
getUsableLocalPort()
查找1024~65535范围内的可用端口
此方法只检测给定范围内的随机一个端口,检测65535-1024次 来自org.springframework.util.SocketUtils |
static int |
getUsableLocalPort(int minPort)
查找指定范围内的可用端口,最大值为65535
此方法只检测给定范围内的随机一个端口,检测65535-minPort次 来自org.springframework.util.SocketUtils |
static int |
getUsableLocalPort(int minPort,
int maxPort)
查找指定范围内的可用端口
此方法只检测给定范围内的随机一个端口,检测maxPort-minPort次 来自org.springframework.util.SocketUtils |
static TreeSet<Integer> |
getUsableLocalPorts(int numRequested,
int minPort,
int maxPort)
获取多个本地可用端口
来自org.springframework.util.SocketUtils |
static String |
hideIpPart(long ip)
隐藏掉IP地址的最后一部分为 * 代替
|
static String |
hideIpPart(String ip)
隐藏掉IP地址的最后一部分为 * 代替
|
static String |
idnToASCII(String unicode)
Unicode域名转puny code
|
static long |
ipv4ToLong(String strIP)
根据ip地址计算出long型的数据
|
static boolean |
isInnerIP(String ipAddress)
判定是否为内网IPv4
私有IP: A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类 192.168.0.0-192.168.255.255 当然,还有127这个网段是环回地址 |
static boolean |
isInRange(String ip,
String cidr)
是否在CIDR规则配置范围内
方法来自:【成都】小邓 |
static boolean |
isOpen(InetSocketAddress address,
int timeout)
检查远程端口是否开启
|
static boolean |
isUnknown(String checkString)
检测给定字符串是否为未知,多用于检测HTTP请求相关
|
static boolean |
isUsableLocalPort(int port)
检测本地端口可用性
来自org.springframework.util.SocketUtils |
static boolean |
isValidPort(int port)
是否为有效的端口
此方法并不检查端口是否被占用 |
static LinkedHashSet<InetAddress> |
localAddressList(Predicate<InetAddress> addressPredicate)
获取所有满足过滤条件的本地IP地址对象
|
static LinkedHashSet<InetAddress> |
localAddressList(Predicate<NetworkInterface> networkInterfaceFilter,
Predicate<InetAddress> addressPredicate)
获取所有满足过滤条件的本地IP地址对象
|
static LinkedHashSet<String> |
localIps()
获得本机的IP地址列表(包括Ipv4和Ipv6)
返回的IP列表有序,按照系统设备顺序 |
static LinkedHashSet<String> |
localIpv4s()
获得本机的IPv4地址列表
返回的IP列表有序,按照系统设备顺序 |
static String |
longToIpv4(long longIP)
根据long值获取ip v4地址
|
static void |
netCat(String host,
int port,
boolean isBlock,
ByteBuffer data)
简易的使用Socket发送数据
|
static void |
netCat(String host,
int port,
byte[] data)
使用普通Socket发送数据
|
static List<HttpCookie> |
parseCookies(String cookieStr)
解析Cookie信息
|
static boolean |
ping(String ip)
检测IP地址是否能ping通
|
static boolean |
ping(String ip,
int timeout)
检测IP地址是否能ping通
|
static void |
setGlobalAuthenticator(Authenticator authenticator)
设置全局验证
|
static void |
setGlobalAuthenticator(String user,
char[] pass)
设置全局验证
|
static String |
toAbsoluteUrl(String absoluteBasePath,
String relativePath)
相对URL转换为绝对URL
|
static LinkedHashSet<String> |
toIpList(Set<InetAddress> addressList)
地址列表转换为IP地址列表
|
public static final String LOCAL_IP
public static final int PORT_RANGE_MIN
public static final int PORT_RANGE_MAX
public static String longToIpv4(long longIP)
longIP - IP的long表示形式Ipv4Util.longToIpv4(long)public static long ipv4ToLong(String strIP)
strIP - IP V4 地址Ipv4Util.ipv4ToLong(String)public static boolean isUsableLocalPort(int port)
port - 被检测的端口public static boolean isValidPort(int port)
port - 端口号public static int getUsableLocalPort()
public static int getUsableLocalPort(int minPort)
minPort - 端口最小值(包含)public static int getUsableLocalPort(int minPort,
int maxPort)
minPort - 端口最小值(包含)maxPort - 端口最大值(包含)public static TreeSet<Integer> getUsableLocalPorts(int numRequested, int minPort, int maxPort)
numRequested - 尝试次数minPort - 端口最小值(包含)maxPort - 端口最大值(包含)public static boolean isInnerIP(String ipAddress)
A类 10.0.0.0-10.255.255.255 B类 172.16.0.0-172.31.255.255 C类 192.168.0.0-192.168.255.255当然,还有127这个网段是环回地址
ipAddress - IP地址Ipv4Util.isInnerIP(String)public static String toAbsoluteUrl(String absoluteBasePath, String relativePath)
absoluteBasePath - 基准路径,绝对relativePath - 相对路径public static String hideIpPart(String ip)
ip - IP地址public static String hideIpPart(long ip)
ip - IP地址public static String getIpByHost(String hostName)
hostName - HOSTpublic static NetworkInterface getNetworkInterface(String name)
name - 网络接口名,例如Linux下默认是eth0nullpublic static Collection<NetworkInterface> getNetworkInterfaces()
nullpublic static LinkedHashSet<String> localIpv4s()
LinkedHashSetpublic static LinkedHashSet<String> toIpList(Set<InetAddress> addressList)
addressList - 地址Inet4Address 列表public static LinkedHashSet<String> localIps()
LinkedHashSetpublic static LinkedHashSet<InetAddress> localAddressList(Predicate<InetAddress> addressPredicate)
addressPredicate - 过滤器,Predicate.test(Object)为true保留,null表示不过滤,获取所有地址public static LinkedHashSet<InetAddress> localAddressList(Predicate<NetworkInterface> networkInterfaceFilter, Predicate<InetAddress> addressPredicate)
networkInterfaceFilter - 过滤器,null表示不过滤,获取所有网卡addressPredicate - 过滤器,Predicate.test(Object)为true保留,null表示不过滤,获取所有地址public static String getLocalhostStrV4()
InetAddress.getLocalHost()方法获取。null参考: http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
nullpublic static InetAddress getLocalhostV4()
InetAddress.getLocalHost() 获取地址
此方法不会抛出异常,获取失败将返回null
见:https://github.com/dromara/hutool/issues/428
nullpublic static String getLocalMacAddressV4()
public static InetSocketAddress createAddress(String host, int port)
host - 域名或IP地址,空表示任意地址port - 端口,0表示系统分配临时端口InetSocketAddresspublic static void netCat(String host, int port, boolean isBlock, ByteBuffer data) throws IORuntimeException
host - Server主机port - Server端口isBlock - 是否阻塞方式data - 需要发送的数据IORuntimeException - IO异常public static void netCat(String host, int port, byte[] data) throws IORuntimeException
host - Server主机port - Server端口data - 数据IORuntimeException - IO异常public static boolean isInRange(String ip, String cidr)
ip - 需要验证的IPcidr - CIDR规则public static String idnToASCII(String unicode)
unicode - Unicode域名public static String getMultistageReverseProxyIp(String ip)
ip - 获得的IP地址public static boolean isUnknown(String checkString)
checkString - 被检测的字符串public static boolean ping(String ip)
ip - IP地址public static boolean ping(String ip, int timeout)
ip - IP地址timeout - 检测超时(毫秒)public static List<HttpCookie> parseCookies(String cookieStr)
cookieStr - Cookie字符串public static boolean isOpen(InetSocketAddress address, int timeout)
address - 远程地址timeout - 检测超时public static void setGlobalAuthenticator(String user, char[] pass)
user - 用户名pass - 密码,考虑安全,此处不使用Stringpublic static void setGlobalAuthenticator(Authenticator authenticator)
authenticator - 验证器public static List<String> getDnsInfo(String hostName, String... attrNames)
NetUtil.attrNames("hutool.cn", "TXT")
hostName - 主机域名attrNames - 属性public static String getAddressName(InetAddress address)
null返回nulladdress - InetAddress,提供null返回nullCopyright © 2025. All rights reserved.