电脑IP获取攻略:轻松掌握Java代码获取本地IP地址全方法

在计算机网络中,IP地址是标识设备在网络中的唯一地址。对于Java开发者来说,获取本地电脑的IP地址是一个基础且实用的技能。本文将详细介绍如何在Java中获取本地IP地址,并提供多种方法供您选择。
一、使用InetAddress类获取IP地址
Java的
java.net.InetAddress
类提供了获取IP地址的方法。以下是一个简单的示例:
import java.net.InetAddress;

public class GetLocalIP {
public static void main(String[] args) {
try {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(“Local IP Address: ” + localHost.getHostAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}
这段代码通过调用
InetAddress.getLocalHost()
方法获取本地主机信息,然后通过
getHostAddress()
方法获取IP地址。
二、通过NetworkInterface类获取IP地址
如果您需要获取特定网络接口的IP地址,可以使用
java.net.NetworkInterface
类。以下是一个示例:
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class GetLocalIPByInterface {
public static void main(String[] args) {
try {
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
Enumeration inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress.getHostAddress().contains(“.”)) {
System.out.println(“IP Address: ” + inetAddress.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
这段代码遍历所有网络接口,并获取每个接口的IP地址。注意,这里我们排除了回环地址(loopback address),即127.0.0.1。
三、使用JNA库获取IP地址
如果您需要更底层的操作,可以使用JNA(Java Native Access)库。以下是一个示例:
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

public class GetLocalIPWithJNA {
public static void main(String[] args) {
Pointer ptr = new Pointer();
IntByReference size = new IntByReference();
Native.loadLibrary(“Iphlpapi”, Iphlpapi.class).GetIfTable(ptr, size);
IfTable ifTable = new IfTable(ptr, size.getValue());
for (IfEntry entry : ifTable.getEntries()) {
System.out.println(“IP Address: ” + entry.getIpAddress().getHostAddress());
}
}
}

class Iphlpapi {
static {
Native.register(Iphlpapi.class);
}
public static native int GetIfTable(Pointer pIfTable, IntByReference pSize);
}

class IfTable {
private Pointer pointer;
private int size;

public IfTable(Pointer pointer, int size) {
this.pointer = pointer;
this.size = size;
}

public IfEntry[] getEntries() {
IfEntry[] entries = new IfEntry[size];
for (int i = 0; i < size; i++) { entries[i] = new IfEntry(pointer, i); } return entries; } } class IfEntry { private Pointer pointer; private int index; public IfEntry(Pointer pointer, int index) { this.pointer = pointer; this.index = index; } public InetAddress getIpAddress() { byte[] bytes = new byte[4]; bytes[0] = pointer.getByte(index * 4); bytes[1] = pointer.getByte(index * 4 + 1); bytes[2] = pointer.getByte(index * 4 + 2); bytes[3] = pointer.getByte(index * 4 + 3); return InetAddress.getByAddress(bytes); } } 这段代码使用了JNA库来访问Windows的IP帮助库(Iphlpapi.dll),从而获取所有网络接口的IP地址。 总结 本文介绍了三种在Java中获取本地IP地址的方法。您可以根据自己的需求选择合适的方法。希望本文能帮助您轻松掌握Java代码获取本地IP地址的方法。