在Java中,可以使用InetAddress.getLocalHost()
来获取运行Java应用程序的当前服务器的IP地址。
package com.mkyong;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test {
public static void main(String[] args) {
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());
} catch (UnknownHostException e) {
e.e.printStackTrace();
}
}
}
输出量
Current IP address : 192.168.1.22