Package org.aoju.bus.core.toolkit
Class NetKit
java.lang.Object
org.aoju.bus.core.toolkit.NetKit
网络相关工具
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic 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(String fromIp, String toIp) 计算IP区间有多少个IPstatic intcountByMaskBit(int maskBit, boolean isAll) 计算子网大小static InetSocketAddresscreateAddress(String host, int port) static InitialContextcreateInitialContext(Map<String, String> environment) static InitialDirContextcreateInitialDirContext(Map<String, String> environment) static StringformatIpBlock(String ip, String mask) 格式化IP段static AttributesgetAttributes(String uri, String... attrIds) 获取指定容器环境的对象的属性 如获取DNS属性,则URI为类似:dns:aoju.cnstatic LonggetBeginIpLong(String ip, int maskBit) 根据 ip/掩码位 计算IP段的起始IP(Long型)static StringgetBeginIpStr(String ip, int maskBit) 根据 ip/掩码位 计算IP段的起始IP(字符串型) 方法别名:inet_ntoagetDnsInfo(String hostName, String... attrNames) 获取DNS信息,如TXT信息:static LonggetEndIpLong(String ip, int maskBit) 根据 ip/掩码位 计算IP段的终止IP(Long型) 注:此接口返回负数,请使用转成字符串后再转Long型static StringgetEndIpStr(String ip, int maskBit) 根据 ip/掩码位 计算IP段的终止IP(字符串型)static byte[]getHardwareAddress(InetAddress inetAddress) 获得指定地址信息中的硬件地址static StringgetIpByHost(String hostName) 通过域名得到IPstatic byte[]获得本机物理地址static InetAddress获取本机网卡IP地址,规则如下:static String获取主机名称,一次获取会缓存名称static String获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个 如果获取失败调用InetAddress.getLocalHost()方法获取。 此方法不会抛出异常,获取失败将返回nullstatic 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地址(xxx.xxx.xxx.xxx)计算出long型的数据 方法别名:inet_atonstatic 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) 是否为有效的端口 此方法并不检查端口是否被占用智能转换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 longIP) 根据long值获取ip v4地址:xx.xx.xx.xxstatic 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:
-
-
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
获取多个本地可用端口- Parameters:
numRequested- 尝试次数minPort- 端口最小值(包含)maxPort- 端口最大值(包含)- Returns:
- 可用的端口
-
toAbsoluteUrl
相对URL转换为绝对URL- Parameters:
absoluteBasePath- 基准路径,绝对relativePath- 相对路径- Returns:
- 绝对URL
-
hideIpPart
隐藏掉IP地址的最后一部分为 * 代替- Parameters:
ip- IP地址- Returns:
- 隐藏部分后的IP
-
hideIpPart
隐藏掉IP地址的最后一部分为 * 代替- Parameters:
ip- IP地址- Returns:
- 隐藏部分后的IP
-
buildInetSocketAddress
构建InetSocketAddress 当host中包含端口时(用“:”隔开),使用host中的端口,否则使用默认端口 给定host为空时使用本地host(127.0.0.1)- Parameters:
host- HostdefaultPort- 默认端口- Returns:
- InetSocketAddress
-
getIpByHost
通过域名得到IP- Parameters:
hostName- HOST- Returns:
- ip address or hostName if UnknownHostException
-
getNetworkInterface
获取指定名称的网卡信息- Parameters:
name- 网络接口名,例如Linux下默认是eth0- Returns:
- 网卡,未找到返回
null
-
getNetworkInterfaces
获取本机所有网卡- Returns:
- 所有网卡,异常返回
null
-
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
获得本机MAC地址- Returns:
- 本机MAC地址
-
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
获取主机名称,一次获取会缓存名称- Returns:
- 主机名称
-
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
是否在CIDR规则配置范围内 方法来自:【成都】小邓- Parameters:
ip- 需要验证的IPcidr- CIDR规则- Returns:
- 是否在范围内
-
idnToASCII
Unicode域名转puny code- Parameters:
unicode- Unicode域名- Returns:
- puny code
-
getMultistageReverseProxyIp
从多级反向代理中获得第一个非unknown IP地址- Parameters:
ip- 获得的IP地址- Returns:
- 第一个非unknown IP地址
-
isUnknown
检测给定字符串是否为未知,多用于检测HTTP请求相关- Parameters:
checkString- 被检测的字符串- Returns:
- 是否未知
-
ping
检测IP地址是否能ping通- Parameters:
ip- IP地址- 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
获取DNS信息,如TXT信息:NetKit.attrNames("aoju.org", "TXT")- Parameters:
hostName- 主机域名attrNames- 属性- Returns:
- DNS信息
-
formatIpBlock
格式化IP段- Parameters:
ip- IP地址mask- 掩码- Returns:
- 返回xxx.xxx.xxx.xxx/mask的格式
-
list
智能转换IP地址集合- Parameters:
ipRange- IP段,支持X.X.X.X-X.X.X.X或X.X.X.X/XisAll- true:全量地址,false:可用地址;仅在ipRange为X.X.X.X/X时才生效- Returns:
- IP集
-
list
根据IP地址、子网掩码获取IP地址区间- Parameters:
ip- IP地址maskBit- 掩码位,例如24、32isAll- true:全量地址,false:可用地址- Returns:
- 区间地址
-
list
得到IP地址区间- Parameters:
ipFrom- 开始IPipTo- 结束IP- Returns:
- 区间地址
-
longToIpv4
根据long值获取ip v4地址:xx.xx.xx.xx- Parameters:
longIP- IP的long表示形式- Returns:
- IP V4 地址
-
ipv4ToLong
根据ip地址(xxx.xxx.xxx.xxx)计算出long型的数据 方法别名:inet_aton- Parameters:
strIP- IP V4 地址- Returns:
- long值
-
getBeginIpStr
根据 ip/掩码位 计算IP段的起始IP(字符串型) 方法别名:inet_ntoa- Parameters:
ip- 给定的IP,如218.240.38.69maskBit- 给定的掩码位,如30- Returns:
- 起始IP的字符串表示
-
getBeginIpLong
根据 ip/掩码位 计算IP段的起始IP(Long型)- Parameters:
ip- 给定的IP,如218.240.38.69maskBit- 给定的掩码位,如30- Returns:
- 起始IP的长整型表示
-
getEndIpStr
根据 ip/掩码位 计算IP段的终止IP(字符串型)- Parameters:
ip- 给定的IP,如218.240.38.69maskBit- 给定的掩码位,如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- 掩码位isAll- true:全量地址,false:可用地址- Returns:
- 地址总数
-
getMaskByMaskBit
根据掩码位获取掩码- Parameters:
maskBit- 掩码位- Returns:
- 掩码
-
getMaskByIpRange
根据开始IP与结束IP计算掩码- Parameters:
fromIp- 开始IPtoIp- 结束IP- Returns:
- 掩码x.x.x.x
-
countByIpRange
计算IP区间有多少个IP- Parameters:
fromIp- 开始IPtoIp- 结束IP- Returns:
- IP数量
-
isMaskValid
判断掩码是否合法- Parameters:
mask- 掩码的点分十进制表示,例如 255.255.255.0- Returns:
- true:掩码合法;false:掩码不合法
-
isMaskBitValid
public static boolean isMaskBitValid(int maskBit) 判断掩码位是否合法- Parameters:
maskBit- 掩码位,例如 24- 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
-
getEndIpLong
根据 ip/掩码位 计算IP段的终止IP(Long型) 注:此接口返回负数,请使用转成字符串后再转Long型- Parameters:
ip- 给定的IP,如218.240.38.69maskBit- 给定的掩码位,如30- Returns:
- 终止IP的长整型表示
-
createInitialDirContext
- Parameters:
environment- 环境参数,{code null}表示无参数- Returns:
InitialDirContext
-
createInitialContext
- Parameters:
environment- 环境参数,{code null}表示无参数- Returns:
InitialContext
-
getAttributes
获取指定容器环境的对象的属性 如获取DNS属性,则URI为类似:dns:aoju.cn- Parameters:
uri- URI字符串,格式为[scheme:][name]/[domain]attrIds- 需要获取的属性ID名称- Returns:
Attributes
-