java获取本机ip地址

用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方法表示地址。

上一篇:非springboot java单元测试基类


下一篇:Python logger模块