编程语言
首页 > 编程语言> > java获取本机ip地址

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

标签:info,java,本机,getAddress,ip,getHostName,logger,getHostAddress,localHost
来源: https://blog.csdn.net/enthan809882/article/details/120906034