public class NetUtil extends Object
| 构造器和说明 |
|---|
NetUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static InetSocketAddress |
buildInetSocketAddress(String host,
int defaultPort)
构建InetSocketAddress
当host中包含端口时(用“:”隔开),使用host中的端口,否则使用默认端口 给定host为空时使用本地host(127.0.0.1) |
static InetSocketAddress |
createAddress(String host,
int port)
|
static String |
getIpByHost(String hostName)
通过域名得到IP
|
static InetAddress |
getLocalhost()
获取本机网卡IP地址,这个地址为所有网卡中非回路地址的第一个
如果获取失败调用 InetAddress.getLocalHost()方法获取。 |
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 Collection<NetworkInterface> |
getNetworkInterfaces()
获取本机所有网卡
|
static String |
hideIpPart(long ip)
隐藏掉IP地址的最后一部分为 * 代替
|
static String |
hideIpPart(String ip)
隐藏掉IP地址的最后一部分为 * 代替
|
static long |
ipv4ToLong(String strIP)
根据ip地址计算出long型的数据
|
static boolean |
isInnerIP(String ipAddress)
判定是否为内网IP
私有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 |
isUsableLocalPort(int port)
检测本地端口可用性
|
static boolean |
isValidPort(int port)
是否为有效的端口
|
static LinkedHashSet<String> |
localIpv4s()
获得本机的IP地址列表
返回的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 String |
toAbsoluteUrl(String absoluteBasePath,
String relativePath)
相对URL转换为绝对URL
|
public static String longToIpv4(long longIP)
longIP - IP的long表示形式public static long ipv4ToLong(String strIP)
strIP - IP V4 地址public static boolean isUsableLocalPort(int port)
port - 被检测的端口public static boolean isValidPort(int port)
port - 端口号public static boolean isInnerIP(String ipAddress)
ipAddress - IP地址public static String toAbsoluteUrl(String absoluteBasePath, String relativePath)
absoluteBasePath - 基准路径,绝对relativePath - 相对路径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 Collection<NetworkInterface> getNetworkInterfaces()
nullpublic static LinkedHashSet<String> localIpv4s()
LinkedHashSetpublic static String getLocalhostStr()
InetAddress.getLocalHost()方法获取。nullnullpublic static InetAddress getLocalhost()
InetAddress.getLocalHost()方法获取。nullnullpublic static String getLocalMacAddress()
public static String getMacAddress(InetAddress inetAddress)
inetAddress - InetAddresspublic static String getMacAddress(InetAddress inetAddress, String separator)
inetAddress - InetAddressseparator - 分隔符,推荐使用“-”或者“:”public static InetSocketAddress createAddress(String host, int port)
host - 域名或IP地址port - 端口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 - 数据IOException - IO异常IORuntimeExceptionCopyright © 2018. All rights reserved.