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:

  1. longToIpv4: Get the IPv4 address from a long value
  2. ipv4ToLong: Calculate the long value from an IP address
  3. isUsableLocalPort: Check the availability of a local port
  4. isValidPort: Determine whether a port is valid
  5. isInnerIP: Determine whether an IP address is a private IP
  6. localIpv4s: Get a list of IP addresses for the local machine
  7. toAbsoluteUrl: Convert a relative URL to an absolute URL
  8. hideIpPart: Hide the last part of the IP address and replace it with *
  9. buildInetSocketAddress: Build an InetSocketAddress
  10. getIpByHost: Get the IP address from a domain name
  11. isInner: 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:

API Documentation - NetUtil