Class NetKit

java.lang.Object
org.aoju.bus.core.toolkit.NetKit

public class NetKit extends Object
网络相关工具
Since:
Java 17+
Author:
Kimi Liu
  • 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

      public static BigInteger ipv6ToBigInteger(String ipv6Str)
      将IPv6地址字符串转为大整数
      Parameters:
      ipv6Str - 字符串
      Returns:
      大整数, 如发生异常返回 null
    • bigIntegerToIPv6

      public static String bigIntegerToIPv6(BigInteger bigInteger)
      将大整数转换成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

      public static TreeSet<Integer> getUsableLocalPorts(int numRequested, int minPort, int maxPort)
      获取多个本地可用端口
      Parameters:
      numRequested - 尝试次数
      minPort - 端口最小值(包含)
      maxPort - 端口最大值(包含)
      Returns:
      可用的端口
    • toAbsoluteUrl

      public static String toAbsoluteUrl(String absoluteBasePath, String relativePath)
      相对URL转换为绝对URL
      Parameters:
      absoluteBasePath - 基准路径,绝对
      relativePath - 相对路径
      Returns:
      绝对URL
    • hideIpPart

      public static String hideIpPart(String ip)
      隐藏掉IP地址的最后一部分为 * 代替
      Parameters:
      ip - IP地址
      Returns:
      隐藏部分后的IP
    • hideIpPart

      public static String hideIpPart(long ip)
      隐藏掉IP地址的最后一部分为 * 代替
      Parameters:
      ip - IP地址
      Returns:
      隐藏部分后的IP
    • buildInetSocketAddress

      public static InetSocketAddress buildInetSocketAddress(String host, int defaultPort)
      构建InetSocketAddress 当host中包含端口时(用“:”隔开),使用host中的端口,否则使用默认端口 给定host为空时使用本地host(127.0.0.1)
      Parameters:
      host - Host
      defaultPort - 默认端口
      Returns:
      InetSocketAddress
    • getIpByHost

      public static String getIpByHost(String hostName)
      通过域名得到IP
      Parameters:
      hostName - HOST
      Returns:
      ip address or hostName if UnknownHostException
    • getNetworkInterface

      public static NetworkInterface getNetworkInterface(String name)
      获取指定名称的网卡信息
      Parameters:
      name - 网络接口名,例如Linux下默认是eth0
      Returns:
      网卡,未找到返回null
    • getNetworkInterfaces

      public static Collection<NetworkInterface> getNetworkInterfaces()
      获取本机所有网卡
      Returns:
      所有网卡,异常返回null
    • localIpv4s

      public static LinkedHashSet<String> localIpv4s()
      获得本机的IPv4地址列表 返回的IP列表有序,按照系统设备顺序
      Returns:
      IP地址列表 LinkedHashSet
    • localIpv6s

      public static LinkedHashSet<String> localIpv6s()
      获得本机的IPv6地址列表 返回的IP列表有序,按照系统设备顺序
      Returns:
      IP地址列表 LinkedHashSet
    • toIpList

      public static LinkedHashSet<String> toIpList(Set<InetAddress> addressList)
      地址列表转换为IP地址列表
      Parameters:
      addressList - 地址Inet4Address 列表
      Returns:
      IP地址字符串列表
    • localIps

      public static LinkedHashSet<String> localIps()
      获得本机的IP地址列表(包括Ipv4和Ipv6) 返回的IP列表有序,按照系统设备顺序
      Returns:
      IP地址列表 LinkedHashSet
    • localAddressList

      public static LinkedHashSet<InetAddress> localAddressList(Predicate<InetAddress> addressPredicate)
      获取所有满足过滤条件的本地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

      public static String getLocalhostString()
      获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个 如果获取失败调用 InetAddress.getLocalHost()方法获取。 此方法不会抛出异常,获取失败将返回null

      参考: http://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java

      Returns:
      本机网卡IP地址,获取失败返回null
    • getLocalhost

      public static InetAddress getLocalhost()
      获取本机网卡IP地址,规则如下:
       1. 查找所有网卡地址,必须非回路(loopback)地址、非局域网地址(siteLocal)、IPv4地址
       2. 如果无满足要求的地址,调用 InetAddress.getLocalHost() 获取地址
       

      此方法不会抛出异常,获取失败将返回null

      Returns:
      本机网卡IP地址, 获取失败返回null
    • getLocalMacAddress

      public static String getLocalMacAddress()
      获得本机MAC地址
      Returns:
      本机MAC地址
    • getMacAddress

      public static String getMacAddress(InetAddress inetAddress)
      获得指定地址信息中的MAC地址,使用分隔符“-”
      Parameters:
      inetAddress - InetAddress
      Returns:
      MAC地址,用-分隔
    • getMacAddress

      public static String getMacAddress(InetAddress inetAddress, String separator)
      获得指定地址信息中的MAC地址
      Parameters:
      inetAddress - InetAddress
      separator - 分隔符,推荐使用“-”或者“:”
      Returns:
      MAC地址,用-分隔
    • getHardwareAddress

      public static byte[] getHardwareAddress(InetAddress inetAddress)
      获得指定地址信息中的硬件地址
      Parameters:
      inetAddress - InetAddress
      Returns:
      硬件地址
    • getLocalHardwareAddress

      public static byte[] getLocalHardwareAddress()
      获得本机物理地址
      Returns:
      本机物理地址
    • getLocalHostName

      public static String getLocalHostName()
      获取主机名称,一次获取会缓存名称
      Returns:
      主机名称
    • createAddress

      public static InetSocketAddress createAddress(String host, int port)
      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

      public static void netCat(String host, int port, byte[] data) throws InternalException
      使用普通Socket发送数据
      Parameters:
      host - Server主机
      port - Server端口
      data - 数据
      Throws:
      InternalException - IO异常
    • isInRange

      public static boolean isInRange(String ip, String cidr)
      是否在CIDR规则配置范围内 方法来自:【成都】小邓
      Parameters:
      ip - 需要验证的IP
      cidr - CIDR规则
      Returns:
      是否在范围内
    • idnToASCII

      public static String idnToASCII(String unicode)
      Unicode域名转puny code
      Parameters:
      unicode - Unicode域名
      Returns:
      puny code
    • getMultistageReverseProxyIp

      public static String getMultistageReverseProxyIp(String ip)
      从多级反向代理中获得第一个非unknown IP地址
      Parameters:
      ip - 获得的IP地址
      Returns:
      第一个非unknown IP地址
    • isUnknown

      public static boolean isUnknown(String checkString)
      检测给定字符串是否为未知,多用于检测HTTP请求相关
      Parameters:
      checkString - 被检测的字符串
      Returns:
      是否未知
    • ping

      public static boolean ping(String ip)
      检测IP地址是否能ping通
      Parameters:
      ip - IP地址
      Returns:
      返回是否ping通
    • ping

      public static boolean ping(String ip, int timeout)
      检测IP地址是否能ping通
      Parameters:
      ip - IP地址
      timeout - 检测超时(毫秒)
      Returns:
      是否ping通
    • parseCookies

      public static List<HttpCookie> parseCookies(String cookieStr)
      解析Cookie信息
      Parameters:
      cookieStr - Cookie字符串
      Returns:
      cookie字符串
    • isOpen

      public static boolean isOpen(InetSocketAddress address, int timeout)
      检查远程端口是否开启
      Parameters:
      address - 远程地址
      timeout - 检测超时
      Returns:
      远程端口是否开启
    • setGlobalAuthenticator

      public static void setGlobalAuthenticator(Authenticator authenticator)
      设置全局验证
      Parameters:
      authenticator - 验证器
    • getDnsInfo

      public static List<String> getDnsInfo(String hostName, String... attrNames)
      获取DNS信息,如TXT信息:
           NetKit.attrNames("aoju.org", "TXT")
       
      Parameters:
      hostName - 主机域名
      attrNames - 属性
      Returns:
      DNS信息
    • formatIpBlock

      public static String formatIpBlock(String ip, String mask)
      格式化IP段
      Parameters:
      ip - IP地址
      mask - 掩码
      Returns:
      返回xxx.xxx.xxx.xxx/mask的格式
    • list

      public static List<String> list(String ipRange, boolean isAll)
      智能转换IP地址集合
      Parameters:
      ipRange - IP段,支持X.X.X.X-X.X.X.X或X.X.X.X/X
      isAll - true:全量地址,false:可用地址;仅在ipRange为X.X.X.X/X时才生效
      Returns:
      IP集
    • list

      public static List<String> list(String ip, int maskBit, boolean isAll)
      根据IP地址、子网掩码获取IP地址区间
      Parameters:
      ip - IP地址
      maskBit - 掩码位,例如24、32
      isAll - true:全量地址,false:可用地址
      Returns:
      区间地址
    • list

      public static List<String> list(String ipFrom, String ipTo)
      得到IP地址区间
      Parameters:
      ipFrom - 开始IP
      ipTo - 结束IP
      Returns:
      区间地址
    • longToIpv4

      public static String longToIpv4(long longIP)
      根据long值获取ip v4地址:xx.xx.xx.xx
      Parameters:
      longIP - IP的long表示形式
      Returns:
      IP V4 地址
    • ipv4ToLong

      public static long ipv4ToLong(String strIP)
      根据ip地址(xxx.xxx.xxx.xxx)计算出long型的数据 方法别名:inet_aton
      Parameters:
      strIP - IP V4 地址
      Returns:
      long值
    • getBeginIpStr

      public static String getBeginIpStr(String ip, int maskBit)
      根据 ip/掩码位 计算IP段的起始IP(字符串型) 方法别名:inet_ntoa
      Parameters:
      ip - 给定的IP,如218.240.38.69
      maskBit - 给定的掩码位,如30
      Returns:
      起始IP的字符串表示
    • getBeginIpLong

      public static Long getBeginIpLong(String ip, int maskBit)
      根据 ip/掩码位 计算IP段的起始IP(Long型)
      Parameters:
      ip - 给定的IP,如218.240.38.69
      maskBit - 给定的掩码位,如30
      Returns:
      起始IP的长整型表示
    • getEndIpStr

      public static String getEndIpStr(String ip, int maskBit)
      根据 ip/掩码位 计算IP段的终止IP(字符串型)
      Parameters:
      ip - 给定的IP,如218.240.38.69
      maskBit - 给定的掩码位,如30
      Returns:
      终止IP的字符串表示
    • getMaskBitByMask

      public static int getMaskBitByMask(String mask)
      根据子网掩码转换为掩码位
      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

      public static String getMaskByMaskBit(int maskBit)
      根据掩码位获取掩码
      Parameters:
      maskBit - 掩码位
      Returns:
      掩码
    • getMaskByIpRange

      public static String getMaskByIpRange(String fromIp, String toIp)
      根据开始IP与结束IP计算掩码
      Parameters:
      fromIp - 开始IP
      toIp - 结束IP
      Returns:
      掩码x.x.x.x
    • countByIpRange

      public static int countByIpRange(String fromIp, String toIp)
      计算IP区间有多少个IP
      Parameters:
      fromIp - 开始IP
      toIp - 结束IP
      Returns:
      IP数量
    • isMaskValid

      public static boolean isMaskValid(String mask)
      判断掩码是否合法
      Parameters:
      mask - 掩码的点分十进制表示,例如 255.255.255.0
      Returns:
      true:掩码合法;false:掩码不合法
    • isMaskBitValid

      public static boolean isMaskBitValid(int maskBit)
      判断掩码位是否合法
      Parameters:
      maskBit - 掩码位,例如 24
      Returns:
      true:掩码位合法;false:掩码位不合法
    • isInnerIP

      public 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这个网段是环回地址
      Parameters:
      ipAddress - IP地址
      Returns:
      是否为内网IP
    • getEndIpLong

      public static Long getEndIpLong(String ip, int maskBit)
      根据 ip/掩码位 计算IP段的终止IP(Long型) 注:此接口返回负数,请使用转成字符串后再转Long型
      Parameters:
      ip - 给定的IP,如218.240.38.69
      maskBit - 给定的掩码位,如30
      Returns:
      终止IP的长整型表示
    • createInitialDirContext

      public static InitialDirContext createInitialDirContext(Map<String,String> environment)
      Parameters:
      environment - 环境参数,{code null}表示无参数
      Returns:
      InitialDirContext
    • createInitialContext

      public static InitialContext createInitialContext(Map<String,String> environment)
      Parameters:
      environment - 环境参数,{code null}表示无参数
      Returns:
      InitialContext
    • getAttributes

      public static Attributes getAttributes(String uri, String... attrIds)
      获取指定容器环境的对象的属性 如获取DNS属性,则URI为类似:dns:aoju.cn
      Parameters:
      uri - URI字符串,格式为[scheme:][name]/[domain]
      attrIds - 需要获取的属性ID名称
      Returns:
      Attributes