zoukankan      html  css  js  c++  java
  • Java获取本地IP地址和主机名

    方式一:通过java.net.InetAddress类获取

    package com.liyh.test;
    import org.junit.Test; import java.net.InetAddress; import java.net.UnknownHostException; public class Test { @Test public void test1(){ try { InetAddress addr = InetAddress.getLocalHost(); System.out.println("IP地址:" + addr.getHostAddress() + ",主机名:" + addr.getHostName()); } catch (UnknownHostException e) { e.printStackTrace(); } }

    输出:

    IP地址:169.254.229.1,主机名:LAPTOP-J97BNT3M

    这种方式获取到的主机名没啥问题,这种方式获取的主机名没啥问题,但获取到的IP地址却有待考量,如果一台机器有多个网卡,他获取的IP是谁的呢?

    事实上,上面输出的IP是本地/虚拟机IP地址,既不是有线网卡的地址,也不是无线网卡的地址!!!!

    方式二:利用java.net.NetworkInterface获取

    @Test
     public void test2(){
        try {
            Enumeration<NetworkInterface> faces = NetworkInterface.getNetworkInterfaces();
         //遍历网络地址
            while (faces.hasMoreElements()) {
                NetworkInterface face = faces.nextElement();
                if (face.isLoopback() || face.isVirtual() || !face.isUp()) {
                    continue;
                }
                System.out.print("网络接口名:" + face.getDisplayName() + ",地址:");
                Enumeration<InetAddress> address = face.getInetAddresses();
           //遍历网络地址
                while (address.hasMoreElements()) { 
                    InetAddress addr = address.nextElement();
                    if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress() && !addr.isAnyLocalAddress()) {
                        System.out.print(addr.getHostAddress() + " ");
                    }
                }
                System.out.println("");
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }

    输出:

    网络接口名:VMware Virtual Ethernet Adapter for VMnet8,地址:192.168.2.137
    网络接口名:TAP-Windows Adapter V9,地址:10.2.0.40 
    网络接口名:VMware Virtual Ethernet Adapter for VMnet1,地址:192.168.10.23 
    网络接口名:Intel(R) Dual Band Wireless-AC 8265,地址:192.168.78.27 

     

    工具类:

    package com.liyh.utils;
    import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; /** * 本地主机工具类 * * @author li * @since 2020-08-07:15:35:19 * */ public class LocalHostUtil { /** * 获取主机名称 * * @return * @throws UnknownHostException */ public static String getHostName() throws UnknownHostException { return InetAddress.getLocalHost().getHostName(); } /** * 获取系统首选IP * * @return * @throws UnknownHostException */ public static String getLocalIP() throws UnknownHostException { return InetAddress.getLocalHost().getHostAddress(); } /** * 获取所有网卡IP,排除回文地址、虚拟地址 * * @return * @throws SocketException */ public static String[] getLocalIPs() throws SocketException { List<String> list = new ArrayList<>(); Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface intf = enumeration.nextElement(); if (intf.isLoopback() || intf.isVirtual()) { // continue; } Enumeration<InetAddress> inets = intf.getInetAddresses(); while (inets.hasMoreElements()) { InetAddress addr = inets.nextElement(); if (addr.isLoopbackAddress() || !addr.isSiteLocalAddress() || addr.isAnyLocalAddress()) { continue; } list.add(addr.getHostAddress()); } } return list.toArray(new String[0]); } /** * 判断操作系统是否是Windows * * @return */ public static boolean isWindowsOS() { boolean isWindowsOS = false; String osName = System.getProperty("os.name"); if (osName.toLowerCase().indexOf("windows") > -1) { isWindowsOS = true; } return isWindowsOS; } public static void main(String[] args) { try { System.out.println("主机是否为Windows系统:" + LocalHostUtil.isWindowsOS()); System.out.println("主机名称:" + LocalHostUtil.getHostName()); System.out.println("系统首选IP:" + LocalHostUtil.getLocalIP()); System.out.println("系统所有IP:" + String.join(",", LocalHostUtil.getLocalIPs())); } catch (UnknownHostException e) { } catch (Exception e) { e.printStackTrace(); } } }
  • 相关阅读:
    C++网络编程服务器select模型(参考)
    javascript中多维数组的使用
    C++ 网络编程 阻塞I/O模型并发回显服务器
    指针在javascript的使用方式
    C++网络编程之服务器编程
    C++网络编程之客户端程序
    Excel数据提取C++代码(仅供参考)
    SkipOut游戏实现代码
    C++读取Excel 精华
    MFC excel修改类
  • 原文地址:https://www.cnblogs.com/liyhbk/p/13453248.html
Copyright © 2011-2022 走看看