public class NetUtil extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
LOCAL_IP
本地IPv4地址
|
static String |
localhostName
本地主机名称
|
static int |
PORT_RANGE_MAX
默认最大端口,65535
|
static int |
PORT_RANGE_MIN
默认最小端口,1024
|
| Constructor and Description |
|---|
NetUtil() |
| Modifier and Type | Method and Description |
|---|---|
static String |
bigIntegerToIPv6(BigInteger bigInteger)
将大整数转换成ipv6字符串
|
static InetSocketAddress |
buildInetSocketAddress(String host,
int defaultPort)
构建InetSocketAddress
当host中包含端口时(用“:”隔开),使用host中的端口,否则使用默认端口 给定host为空时使用本地host(127.0.0.1) |
static InetSocketAddress |
createAddress(String host,
int port)
|
static List<String> |
getDnsInfo(String hostName,
String... attrNames)
获取DNS信息,如TXT信息:
NetUtil.attrNames("hutool.cn", "TXT")
|
static byte[] |
getHardwareAddress(InetAddress inetAddress)
获得指定地址信息中的硬件地址
|
static String |
getIpByHost(String hostName)
通过域名得到IP
|
static byte[] |
getLocalHardwareAddress()
获得本机物理地址
|
static InetAddress |
getLocalhost()
获取本机网卡IP地址,规则如下:
1.
|
static String |
getLocalHostName()
获取主机名称,一次获取会缓存名称
注意此方法会触发反向DNS解析,导致阻塞,阻塞时间取决于网络! |
static String |
getLocalhostStr()
获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个
如果获取失败调用 InetAddress.getLocalHost()方法获取。 |
static String |
getLocalMacAddress()
获得本机MAC地址
|
static String |
getMacAddress(InetAddress inetAddress)
获得指定地址信息中的MAC地址,使用分隔符“-”
|
static String |
getMacAddress(InetAddress inetAddress,
String separator)
获得指定地址信息中的MAC地址
|
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 BigInteger |
ipv6ToBigInteger(String ipv6Str)
将IPv6地址字符串转为大整数
|
static BigInteger |
ipv6ToBitInteger(String ipv6Str)
Deprecated.
拼写错误,请使用
ipv6ToBigInteger(String) |
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(Filter<InetAddress> addressFilter)
获取所有满足过滤条件的本地IP地址对象
|
static LinkedHashSet<InetAddress> |
localAddressList(Filter<NetworkInterface> networkInterfaceFilter,
Filter<InetAddress> addressFilter)
获取所有满足过滤条件的本地IP地址对象
|
static LinkedHashSet<String> |
localIps()
获得本机的IP地址列表(包括Ipv4和Ipv6)
返回的IP列表有序,按照系统设备顺序 |
static LinkedHashSet<String> |
localIpv4s()
获得本机的IPv4地址列表
返回的IP列表有序,按照系统设备顺序 |
static LinkedHashSet<String> |
localIpv6s()
获得本机的IPv6地址列表
返回的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 String localhostName
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)@Deprecated public static BigInteger ipv6ToBitInteger(String ipv6Str)
ipv6ToBigInteger(String)ipv6Str - 字符串public static BigInteger ipv6ToBigInteger(String ipv6Str)
ipv6Str - 字符串public static String bigIntegerToIPv6(BigInteger bigInteger)
bigInteger - 大整数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 InetSocketAddress buildInetSocketAddress(String host, int defaultPort)
host - HostdefaultPort - 默认端口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> localIpv6s()
LinkedHashSetpublic static LinkedHashSet<String> toIpList(Set<InetAddress> addressList)
addressList - 地址Inet4Address 列表public static LinkedHashSet<String> localIps()
LinkedHashSetpublic static LinkedHashSet<InetAddress> localAddressList(Filter<InetAddress> addressFilter)
addressFilter - 过滤器,null表示不过滤,获取所有地址public static LinkedHashSet<InetAddress> localAddressList(Filter<NetworkInterface> networkInterfaceFilter, Filter<InetAddress> addressFilter)
addressFilter - 过滤器,null表示不过滤,获取所有地址networkInterfaceFilter - 过滤器,null表示不过滤,获取所有网卡public static String getLocalhostStr()
InetAddress.getLocalHost()方法获取。null参考:http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
nullpublic static InetAddress getLocalhost()
1. 查找所有网卡地址,必须非回路(loopback)地址、非局域网地址(siteLocal)、IPv4地址
2. 如果无满足要求的地址,调用 InetAddress.getLocalHost() 获取地址
此方法不会抛出异常,获取失败将返回null
见:https://github.com/chinabugotech/hutool/issues/428
nullpublic static String getLocalMacAddress()
public static String getMacAddress(InetAddress inetAddress)
inetAddress - InetAddresspublic static String getMacAddress(InetAddress inetAddress, String separator)
inetAddress - InetAddressseparator - 分隔符,推荐使用“-”或者“:”public static byte[] getHardwareAddress(InetAddress inetAddress)
inetAddress - InetAddresspublic static byte[] getLocalHardwareAddress()
public static String getLocalHostName()
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 - 验证器Copyright © 2025. All rights reserved.