欢迎使用CSDN-markdown编辑器

来源:互联网 发布:p2p限速软件下载 编辑:程序博客网 时间:2024/05/10 07:37

使用Java获取本地IP

public static String getNativeIP() throws UnknownHostException    {        InetAddress inetAddress = InetAddress.getLocalHost();        if(null != inetAddress && !inetAddress.isLoopbackAddress() &&             !inetAddress.isSiteLocalAddress())        {            return inetAddress.getHostAddress();        }else        {            return null;        }    }

这个地方有几个需要注意的地方,在linux系统中
1. 设置本机名称:hostname 主机名
2. 在/etc/hosts里加一行 本机IP 主机名
3. 用InetAddress.getLocalHost().getHostAddress()测试一下结果是否是与本机IP一致

0 0