获取服务器IP地址

/**
* 获取服务器IP地址
* @return
*/
public static String getServerIp(){
String SERVER_IP = null;
try {
String ipAddr="";
InetAddress inetAddress=InetAddress.getLocalHost();
byte[] adds = inetAddress.getAddress();
for (int i = 0; i < adds.length; i++) {
if(i>0){
ipAddr+=".";
}
ipAddr+=adds[i]&0xFF;
String hostName = inetAddress.getHostName();
System.out.println("ipAddr:"+ipAddr);
System.out.println("hostName:"+hostName);

}
} catch (UnknownHostException e) {
e.printStackTrace();
}
return SERVER_IP;
}

posted @ 2019-04-16 15:16  Sunray0330  阅读(761)  评论(0编辑  收藏  举报