笔记_blog 2021-03-07 00:54 采纳率: 0%
浏览 803

使用Java获取本机IP出错 java.net.UnknownHostException

使用这段代码获取IP 就会报错 


InetAddress.getLocalHost().getHostName();

错误堆栈信息:

00:50:56.166 [main] ERROR RocketmqCommon - Failed to obtain the host name
java.net.UnknownHostException: HEIHEI: HEIHEI
	at java.net.InetAddress.getLocalHost(InetAddress.java:1505) ~[na:1.8.0_131]
	at org.apache.rocketmq.common.BrokerConfig.localHostName(BrokerConfig.java:200) [classes/:na]
	at org.apache.rocketmq.common.BrokerConfig.<init>(BrokerConfig.java:40) [classes/:na]
	at org.apache.rocketmq.broker.BrokerStartup.createBrokerController(BrokerStartup.java:124) [classes/:na]
	at org.apache.rocketmq.broker.BrokerStartup.main(BrokerStartup.java:69) [classes/:na]
Caused by: java.net.UnknownHostException: HEIHEI
	at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_131]
	at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_131]
	at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_131]
	at java.net.InetAddress.getLocalHost(InetAddress.java:1500) ~[na:1.8.0_131]
	... 4 common frames omitted

Process finished with exit code -3

电脑: win 10  求大佬帮忙看看,跪谢。

  • 写回答

1条回答 默认 最新

  • 关注

    不建议使用这种方式获取ip,你可以试试这个

    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.UnknownHostException;
    import java.util.Enumeration;
    
    public class IP {
        public static void main(String[] args) throws UnknownHostException {
            
            System.out.println(getLocalHostExactAddress().getHostAddress());
        }
    
        public static InetAddress getLocalHostExactAddress() {
            try {
                InetAddress candidateAddress = null;
    
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaces.hasMoreElements()) {
                    NetworkInterface iface = networkInterfaces.nextElement();
                    // 该网卡接口下的ip会有多个,也需要一个个的遍历,找到自己所需要的
                    for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) {
                        InetAddress inetAddr = inetAddrs.nextElement();
                        // 排除loopback回环类型地址(不管是IPv4还是IPv6 只要是回环地址都会返回true)
                        if (!inetAddr.isLoopbackAddress()) {
                            if (inetAddr.isSiteLocalAddress()) {
                                // 如果是site-local地址,就是它了 就是我们要找的
                                // ~~~~~~~~~~~~~绝大部分情况下都会在此处返回你的ip地址值~~~~~~~~~~~~~
                                return inetAddr;
                            }
    
                            // 若不是site-local地址 那就记录下该地址当作候选
                            if (candidateAddress == null) {
                                candidateAddress = inetAddr;
                            }
    
                        }
                    }
                }
    
                // 如果出去loopback回环地之外无其它地址了,那就回退到原始方案吧
                return candidateAddress == null ? InetAddress.getLocalHost() : candidateAddress;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀