获取当前IP地址是Java开发中常见需求之一,本文将从以下多个方面对获取当前IP地址的Java代码实现做详细的阐述。
一、使用Inet4Address获取当前IP地址
Inet4Address是Java中一个用于表示IPv4地址的类,可以通过该类获取当前主机的IP地址。
import java.net.InetAddress; import java.net.UnknownHostException; public class InetAddressUtil { public static void main(String[] args) throws UnknownHostException { InetAddress inetAddress = InetAddress.getLocalHost(); System.out.println("当前IP地址为:" + inetAddress.getHostAddress()); } }
通过调用getHostAddress()方法即可获取当前主机的IP地址。
二、使用NetworkInterface获取当前IP地址
除了Inet4Address,还可以使用Java提供的NetworkInterface类获取当前主机的IP地址。
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class NetworkInterfaceUtil { public static void main(String[] args) throws SocketException { EnumerationnetworkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); System.out.println("当前IP地址为:" + inetAddress.getHostAddress()); } } } }
通过NetworkInterface获取所有的网络接口,再通过网络接口获取所有的IP地址。
三、使用HttpClient发送GET请求获取当前IP地址
除了使用Java自带的类获取IP地址,还可以通过HttpClient发送GET请求获取当前主机的IP地址。
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class HttpClientUtil { public static void main(String[] args) throws IOException { HttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://myip.ipip.net/"); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); String result = EntityUtils.toString(httpEntity); String ip = result.substring(result.indexOf("[") + 1, result.indexOf("]")); System.out.println("当前IP地址为:" + ip); } }
通过发送GET请求至http://myip.ipip.net/,并从返回结果中解析出当前主机的IP地址。
四、使用Java获取其它网络信息
除了获取当前IP地址,还可以使用Java获取其它网络信息,例如:本机的MAC地址、域名、端口等。
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; import java.util.Enumeration; public class NetworkInfoUtil { public static void main(String[] args) throws UnknownHostException { InetAddress inetAddress = InetAddress.getLocalHost(); System.out.println("当前主机名为:" + inetAddress.getHostName()); System.out.println("当前主机的IP地址为:" + inetAddress.getHostAddress()); Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement(); byte[] macBytes = networkInterface.getHardwareAddress(); if (macBytes != null) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < macBytes.length; i++) { sb.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? "-" : "")); } System.out.println("当前主机的MAC地址为:" + sb.toString()); } Enumeration inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress address = (InetAddress) inetAddresses.nextElement(); System.out.println("当前主机的域名为:" + address.getHostName()); System.out.println("当前主机的端口号为:" + networkInterface.getMTU()); } } } }
通过调用相应的方法即可获取相应的网络信息。
总结
本文从使用Inet4Address获取IP地址、使用NetworkInterface获取IP地址、使用HttpClient发送GET请求获取IP地址、使用Java获取其它网络信息等多个方面对获取当前IP地址的Java代码实现进行了详细的阐述。在实际开发中,可以根据需求选择相应的方式来获取IP地址。