Package org.aoju.bus.core.toolkit
Class NetKit
java.lang.Object
org.aoju.bus.core.toolkit.NetKit
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longA类私有地址的最小值static final longA类私有地址的最大值static final longB类私有地址的最小值static final longB类私有地址的最大值static final longC类私有地址的最小值static final longC类私有地址的最大值static final int默认最大端口,65535static final int默认最小端口,1024 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbigIntegerToIPv6(BigInteger bigInteger) 将大整数转换成ipv6字符串static InetSocketAddressbuildInetSocketAddress(String host, int defaultPort) 构建InetSocketAddress 当host中包含端口时(用“:”隔开),使用host中的端口,否则使用默认端口 给定host为空时使用本地host(127.0.0.1)static intcountByIpRange(long fromIp, long toIp) 获得 指定区间内的 ip数量static intcountByIpRange(String fromIp, String toIp) 获得 指定区间内的 ip数量static intcountByMaskBit(int maskBit, boolean isAll) 获取 子网内的 地址总数static InetSocketAddresscreateAddress(String host, int port) static StringformatIpBlock(String ip, String mask) 根据 ip地址 和 掩码地址 获得 CIDR格式字符串static longgetBeginIpLong(String ip, int maskBit) 根据 ip 和 掩码位 获取 子网的起始IP(Long型)static StringgetBeginIpStr(String ip, int maskBit) 根据 ip 和 掩码位 获取 子网的起始IP(字符串型)getDnsInfo(String hostName, String... attrNames) 获取DNS信息,如TXT信息:static longgetEndIpLong(String ip, int maskBit) 根据 ip 和 掩码位 获取 子网的终止IP(Long型)static StringgetEndIpStr(String ip, int maskBit) 根据 ip 和 掩码位 获取 子网的终止IP(字符串型)static byte[]getHardwareAddress(InetAddress inetAddress) 获得指定地址信息中的硬件地址static StringgetIpByHost(String hostName) 通过域名得到IPstatic byte[]获得本机物理地址static InetAddress获取本机网卡IP地址,规则如下:static String获取主机名称,一次获取会缓存名称static String获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个 如果获取失败调用InetAddress.getLocalHost()方法获取。static String获得本机MAC地址static StringgetMacAddress(InetAddress inetAddress) 获得指定地址信息中的MAC地址,使用分隔符“-”static StringgetMacAddress(InetAddress inetAddress, String separator) 获得指定地址信息中的MAC地址static intgetMaskBitByMask(String mask) 将 子网掩码 转换为 掩码位static StringgetMaskByIpRange(String fromIp, String toIp) 根据 开始IP 与 结束IP 获取 掩码地址static StringgetMaskByMaskBit(int maskBit) 根据 掩码位 获取 掩码地址static String从多级反向代理中获得第一个非unknown IP地址static NetworkInterfacegetNetworkInterface(String name) 获取指定名称的网卡信息static Collection<NetworkInterface> 获取本机所有网卡static int查找1024~65535范围内的可用端口 此方法只检测给定范围内的随机一个端口,检测65535-1024次static intgetUsableLocalPort(int minPort) 查找指定范围内的可用端口,最大值为65535 此方法只检测给定范围内的随机一个端口,检测65535-minPort次static intgetUsableLocalPort(int minPort, int maxPort) 查找指定范围内的可用端口 此方法只检测给定范围内的随机一个端口,检测maxPort-minPort次getUsableLocalPorts(int numRequested, int minPort, int maxPort) 获取多个本地可用端口static StringhideIpPart(long ip) 隐藏掉IP地址的最后一部分为 * 代替static StringhideIpPart(String ip) 隐藏掉IP地址的最后一部分为 * 代替static StringidnToASCII(String unicode) Unicode域名转puny codestatic longipv4ToLong(String strIp) 将 ip字符串 转换为 long值static BigIntegeripv6ToBigInteger(String ipv6Str) 将IPv6地址字符串转为大整数static boolean判定是否为内网IPv4 私有IP:static boolean是否在CIDR规则配置范围内 方法来自:【成都】小邓static booleanisMaskBitValid(int maskBit) 判断掩码位是否合法static booleanisMaskValid(String mask) 判断掩码是否合法static booleanisOpen(InetSocketAddress address, int timeout) 检查远程端口是否开启static boolean检测给定字符串是否为未知,多用于检测HTTP请求相关static booleanisUsableLocalPort(int port) 检测本地端口可用性static booleanisValidPort(int port) 是否为有效的端口 此方法并不检查端口是否被占用list(long ipFrom, long ipTo) 得到指定区间内的所有IP地址智能获取指定区间内的所有IP地址根据 IP地址 和 掩码位数 获取 子网所有ip地址获得 指定区间内 所有ip地址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> 获得本机的IPv4地址列表 返回的IP列表有序,按照系统设备顺序static LinkedHashSet<String> 获得本机的IPv6地址列表 返回的IP列表有序,按照系统设备顺序static StringlongToIpv4(long ip) 根据 ip的long值 获取 ip字符串,即:xxx.xxx.xxx.xxxstatic voidnetCat(String host, int port, boolean isBlock, ByteBuffer data) 简易的使用Socket发送数据static void使用普通Socket发送数据static List<HttpCookie> parseCookies(String cookieStr) 解析Cookie信息static boolean检测IP地址是否能ping通static boolean检测IP地址是否能ping通static voidsetGlobalAuthenticator(Authenticator authenticator) 设置全局验证static StringtoAbsoluteUrl(String absoluteBasePath, String relativePath) 相对URL转换为绝对URLstatic LinkedHashSet<String> toIpList(Set<InetAddress> addressList) 地址列表转换为IP地址列表
-
Field Details
-
PORT_RANGE_MIN
public static final int PORT_RANGE_MIN默认最小端口,1024- See Also:
-
PORT_RANGE_MAX
public static final int PORT_RANGE_MAX默认最大端口,65535- See Also:
-
A_INNER_IP_LONG_BEGIN
public static final long A_INNER_IP_LONG_BEGINA类私有地址的最小值 -
A_INNER_IP_LONG_END
public static final long A_INNER_IP_LONG_ENDA类私有地址的最大值 -
B_INNER_IP_LONG_BEGIN
public static final long B_INNER_IP_LONG_BEGINB类私有地址的最小值 -
B_INNER_IP_LONG_END
public static final long B_INNER_IP_LONG_ENDB类私有地址的最大值 -
C_INNER_IP_LONG_BEGIN
public static final long C_INNER_IP_LONG_BEGINC类私有地址的最小值 -
C_INNER_IP_LONG_END
public static final long C_INNER_IP_LONG_ENDC类私有地址的最大值
-
-
Constructor Details
-
NetKit
public NetKit()
-
-
Method Details
-
ipv6ToBigInteger
将IPv6地址字符串转为大整数- Parameters:
ipv6Str- 字符串- Returns:
- 大整数, 如发生异常返回 null
-
bigIntegerToIPv6
将大整数转换成ipv6字符串- Parameters:
bigInteger- 大整数- Returns:
- IPv6字符串, 如发生异常返回 null
-
isUsableLocalPort
public static boolean isUsableLocalPort(int port) 检测本地端口可用性- Parameters:
port- 被检测的端口- Returns:
- 是否可用
-
isValidPort
public static boolean isValidPort(int port) 是否为有效的端口 此方法并不检查端口是否被占用- Parameters:
port- 端口号- Returns:
- 是否有效
-
getUsableLocalPort
public static int getUsableLocalPort()查找1024~65535范围内的可用端口 此方法只检测给定范围内的随机一个端口,检测65535-1024次- Returns:
- 可用的端口
-
getUsableLocalPort
public static int getUsableLocalPort(int minPort) 查找指定范围内的可用端口,最大值为65535 此方法只检测给定范围内的随机一个端口,检测65535-minPort次- Parameters:
minPort- 端口最小值(包含)- Returns:
- 可用的端口
-
getUsableLocalPort
public static int getUsableLocalPort(int minPort, int maxPort) 查找指定范围内的可用端口 此方法只检测给定范围内的随机一个端口,检测maxPort-minPort次- Parameters:
minPort- 端口最小值(包含)maxPort- 端口最大值(包含)- Returns:
- 可用的端口
-
getUsableLocalPorts
-
toAbsoluteUrl
-
hideIpPart
-
hideIpPart
隐藏掉IP地址的最后一部分为 * 代替- Parameters:
ip- IP地址- Returns:
- 隐藏部分后的IP
-
buildInetSocketAddress
构建InetSocketAddress 当host中包含端口时(用“:”隔开),使用host中的端口,否则使用默认端口 给定host为空时使用本地host(127.0.0.1)- Parameters:
host- HostdefaultPort- 默认端口- Returns:
- InetSocketAddress
-
getIpByHost
-
getNetworkInterface
获取指定名称的网卡信息- Parameters:
name- 网络接口名,例如Linux下默认是eth0- Returns:
- 网卡,未找到返回
null
-
getNetworkInterfaces
-
localIpv4s
获得本机的IPv4地址列表 返回的IP列表有序,按照系统设备顺序- Returns:
- IP地址列表
LinkedHashSet
-
localIpv6s
获得本机的IPv6地址列表 返回的IP列表有序,按照系统设备顺序- Returns:
- IP地址列表
LinkedHashSet
-
toIpList
地址列表转换为IP地址列表- Parameters:
addressList- 地址Inet4Address列表- Returns:
- IP地址字符串列表
-
localIps
获得本机的IP地址列表(包括Ipv4和Ipv6) 返回的IP列表有序,按照系统设备顺序- Returns:
- IP地址列表
LinkedHashSet
-
localAddressList
获取所有满足过滤条件的本地IP地址对象- Parameters:
addressPredicate- 过滤器,Predicate.test(Object)为true保留,null表示不过滤,获取所有地址- Returns:
- 过滤后的地址对象列表
-
localAddressList
public static LinkedHashSet<InetAddress> localAddressList(Predicate<NetworkInterface> networkInterfaceFilter, Predicate<InetAddress> addressPredicate) 获取所有满足过滤条件的本地IP地址对象- Parameters:
networkInterfaceFilter- 过滤器,null表示不过滤,获取所有网卡addressPredicate- 过滤器,Predicate.test(Object)为true保留,null表示不过滤,获取所有地址- Returns:
- 过滤后的地址对象列表
-
getLocalhostString
获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个 如果获取失败调用InetAddress.getLocalHost()方法获取。 此方法不会抛出异常,获取失败将返回null参考: http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java
- Returns:
- 本机网卡IP地址,获取失败返回
null
-
getLocalhost
获取本机网卡IP地址,规则如下:1. 查找所有网卡地址,必须非回路(loopback)地址、非局域网地址(siteLocal)、IPv4地址 2. 如果无满足要求的地址,调用
InetAddress.getLocalHost()获取地址此方法不会抛出异常,获取失败将返回
null- Returns:
- 本机网卡IP地址, 获取失败返回
null
-
getLocalMacAddress
-
getMacAddress
获得指定地址信息中的MAC地址,使用分隔符“-”- Parameters:
inetAddress-InetAddress- Returns:
- MAC地址,用-分隔
-
getMacAddress
获得指定地址信息中的MAC地址- Parameters:
inetAddress-InetAddressseparator- 分隔符,推荐使用“-”或者“:”- Returns:
- MAC地址,用-分隔
-
getHardwareAddress
获得指定地址信息中的硬件地址- Parameters:
inetAddress-InetAddress- Returns:
- 硬件地址
-
getLocalHardwareAddress
public static byte[] getLocalHardwareAddress()获得本机物理地址- Returns:
- 本机物理地址
-
getLocalHostName
-
createAddress
- Parameters:
host- 域名或IP地址,空表示任意地址port- 端口,0表示系统分配临时端口- Returns:
InetSocketAddress
-
netCat
public static void netCat(String host, int port, boolean isBlock, ByteBuffer data) throws InternalException 简易的使用Socket发送数据- Parameters:
host- Server主机port- Server端口isBlock- 是否阻塞方式data- 需要发送的数据- Throws:
InternalException- IO异常
-
netCat
使用普通Socket发送数据- Parameters:
host- Server主机port- Server端口data- 数据- Throws:
InternalException- IO异常
-
isInRange
-
idnToASCII
-
getMultistageReverseProxyIp
-
isUnknown
检测给定字符串是否为未知,多用于检测HTTP请求相关- Parameters:
checkString- 被检测的字符串- Returns:
- 是否未知
-
ping
-
ping
检测IP地址是否能ping通- Parameters:
ip- IP地址timeout- 检测超时(毫秒)- Returns:
- 是否ping通
-
parseCookies
解析Cookie信息- Parameters:
cookieStr- Cookie字符串- Returns:
- cookie字符串
-
isOpen
检查远程端口是否开启- Parameters:
address- 远程地址timeout- 检测超时- Returns:
- 远程端口是否开启
-
setGlobalAuthenticator
设置全局验证- Parameters:
authenticator- 验证器
-
getDnsInfo
-
formatIpBlock
-
list
-
list
-
list
-
list
-
longToIpv4
根据 ip的long值 获取 ip字符串,即:xxx.xxx.xxx.xxx- Parameters:
ip- IP的long表示形式- Returns:
- 点分十进制ip地址
-
ipv4ToLong
将 ip字符串 转换为 long值方法别名:inet_aton
- Parameters:
strIp- ip地址,点分十进制,xxx.xxx.xxx.xxx- Returns:
- ip的long值
-
getBeginIpStr
-
getBeginIpLong
根据 ip 和 掩码位 获取 子网的起始IP(Long型)- Parameters:
ip- 给定的IP,点分十进制,如:xxx.xxx.xxx.xxxmaskBit- 给定的掩码位,如:30- Returns:
- 起始IP的长整型表示
-
getEndIpStr
-
getEndIpLong
根据 ip 和 掩码位 获取 子网的终止IP(Long型)- Parameters:
ip- 给定的IP,点分十进制,如:xxx.xxx.xxx.xxxmaskBit- 给定的掩码位,如:30- Returns:
- 终止IP的长整型表示
-
getMaskBitByMask
将 子网掩码 转换为 掩码位- Parameters:
mask- 掩码的点分十进制表示,例如 255.255.255.0- Returns:
- 掩码位,例如 24
- Throws:
IllegalArgumentException- 子网掩码非法
-
countByMaskBit
public static int countByMaskBit(int maskBit, boolean isAll) 获取 子网内的 地址总数- Parameters:
maskBit- 掩码位,取值范围:[1, 32]isAll- true:全量地址,false:可用地址- Returns:
- 子网内地址总数
-
getMaskByMaskBit
根据 掩码位 获取 掩码地址- Parameters:
maskBit- 掩码位,如:24,取值范围:[1, 32]- Returns:
- 掩码地址,点分十进制,如:255.255.255.0
-
getMaskByIpRange
-
countByIpRange
-
countByIpRange
public static int countByIpRange(long fromIp, long toIp) 获得 指定区间内的 ip数量- Parameters:
fromIp- 开始IP,包含toIp- 结束IP,包含- Returns:
- IP数量
-
isMaskValid
判断掩码是否合法- Parameters:
mask- 掩码的点分十进制表示,例如 255.255.255.0- Returns:
- true:掩码合法;false:掩码不合法
-
isMaskBitValid
public static boolean isMaskBitValid(int maskBit) 判断掩码位是否合法- Parameters:
maskBit- 掩码位,有效范围:[1, 32]- Returns:
- true:掩码位合法;false:掩码位不合法
-
isInnerIP
判定是否为内网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这个网段是环回地址- Parameters:
ipAddress- IP地址,点分十进制- Returns:
- 是否为内网IP
-