可以使用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
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python怎么获取当前ip地址