获取部署项目的服务器IP
public String getServerIp(){
try {
Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) netInterfaces
.nextElement();
ip = (InetAddress) ni.getInetAddresses().nextElement();
SERVER_IP = ip.getHostAddress();
if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {
SERVER_IP = ip.getHostAddress();
break;
} else {
ip = null;
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SERVER_IP;
}
获取本地机器的IP
public static String getLocalIP(){
InetAddress addr = InetAddress.getLocalHost();
byte[] ipAddr = addr.getAddress();
String ip = "";
for (int i = 0; i < ipAddr.length; i++) {
if (i > 0) {
ip += ".";
}
ip += ipAddr[i] & 0xFF;
}
return ip ;
}