NetUtil
Origin
In daily development, network connectivity is indispensable. Some functions that are commonly used include hiding part of the IP address and converting absolute paths to relative paths.
Introduction
The main methods in the NetUtil
tool include:
longToIpv4
: Get the IPv4 address from a long valueipv4ToLong
: Calculate the long value from an IP addressisUsableLocalPort
: Check the availability of a local portisValidPort
: Determine whether a port is validisInnerIP
: Determine whether an IP address is a private IPlocalIpv4s
: Get a list of IP addresses for the local machinetoAbsoluteUrl
: Convert a relative URL to an absolute URLhideIpPart
: Hide the last part of the IP address and replace it with *buildInetSocketAddress
: Build an InetSocketAddressgetIpByHost
: Get the IP address from a domain nameisInner
: Determine whether a specific IP long value falls within a specified range
Usage
String ip= "127.0.0.1";
long iplong = 2130706433L;
// Get IPv4 address from long value
String ip = NetUtil.longToIpv4(iplong);
// Calculate long value from IP address
long ip = NetUtil.ipv4ToLong(ip);
// Check the availability of a local port
boolean result = NetUtil.isUsableLocalPort(6379);
// Determine whether a port is valid
boolean result = NetUtil.isValidPort(6379);
// Hide part of the IP address
String result = NetUtil.hideIpPart(ip);
For more methods: