用java自带的工具就行。
代码:
try { InetAddress localHost = InetAddress.getLocalHost(); logger.info("getHostName:{}",localHost.getHostName()); logger.info("getAddress:{}",localHost.getAddress()); logger.info("getHostAddress:{}",localHost.getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); }
打印的结果为:
getHostName:chushiyun-PC getAddress:[10, 13, 8, 14] getHostAddress:10.13.8.14
所以应该用getHostAddress方法表示地址。