可以使用InetAddress类中的getLocalHost()方法来获取当前IP地址。代码示例如下:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetIPAddress {
public static void main(String[] args) {
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("当前IP地址:" + address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
运行结果:
当前IP地址:192.168.0.100