您的位置:首页 > 编程语言 > Java开发

java web项目获取服务器IP,匹配一台机器做JOB

2016-05-20 17:00 621 查看
获取部署项目的服务器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 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: