基于Java实现获取本地IP地址和主机名
方式一:通过java.net.InetAddress类获取
publicvoidtest1(){ try{ InetAddressaddr=InetAddress.getLocalHost(); System.out.println("IP地址:"+addr.getHostAddress()+",主机名:"+addr.getHostName()); }catch(UnknownHostExceptione){ e.printStackTrace(); } }
输出:
IP地址:192.168.153.1,主机名:DESKTOP-338UP3E
这种方式获取到的主机名没啥问题,这种方式获取的主机名没啥问题,但获取到的IP地址却有待考量,如果一台机器有多个网卡,
他获取的IP是谁的呢?事实上,上面输出的IP是我虚拟机IP地址,既不是我有线网卡的地址,也不是我无线网卡的地址。
方式二:利用java.net.NetworkInterface获取
publicvoidtest2(){ try{ Enumerationfaces=NetworkInterface.getNetworkInterfaces(); while(faces.hasMoreElements()){//遍历网络接口 NetworkInterfaceface=faces.nextElement(); if(face.isLoopback()||face.isVirtual()||!face.isUp()){ continue; } System.out.print("网络接口名:"+face.getDisplayName()+",地址:"); Enumeration address=face.getInetAddresses(); while(address.hasMoreElements()){//遍历网络地址 InetAddressaddr=address.nextElement(); if(!addr.isLoopbackAddress()&&addr.isSiteLocalAddress()&&!addr.isAnyLocalAddress()){ System.out.print(addr.getHostAddress()+""); } } System.out.println(""); } }catch(SocketExceptione){ e.printStackTrace(); } }
输出:
网络接口名:VMwareVirtualEthernetAdapterforVMnet8,地址:192.168.153.1
网络接口名:TAP-WindowsAdapterV9,地址:10.8.0.30
网络接口名:VMwareVirtualEthernetAdapterforVMnet1,地址:192.168.46.1
网络接口名:Intel(R)DualBandWireless-AC8265,地址:172.16.78.27
疑问?:第一、三行为VM虚拟机网络地址,不知为何还在。
工具类:
importjava.net.InetAddress; importjava.net.NetworkInterface; importjava.net.SocketException; importjava.net.UnknownHostException; importjava.util.ArrayList; importjava.util.Enumeration; importjava.util.List; /** *本地主机工具类 * *@authorzhi *@since2019年11月13日09:04:36 * */ publicclassLocalHostUtil{ /** *获取主机名称 * *@return *@throwsUnknownHostException */ publicstaticStringgetHostName()throwsUnknownHostException{ returnInetAddress.getLocalHost().getHostName(); } /** *获取系统首选IP * *@return *@throwsUnknownHostException */ publicstaticStringgetLocalIP()throwsUnknownHostException{ returnInetAddress.getLocalHost().getHostAddress(); } /** *获取所有网卡IP,排除回文地址、虚拟地址 * *@return *@throwsSocketException */ publicstaticString[]getLocalIPs()throwsSocketException{ Listlist=newArrayList<>(); Enumeration enumeration=NetworkInterface.getNetworkInterfaces(); while(enumeration.hasMoreElements()){ NetworkInterfaceintf=enumeration.nextElement(); if(intf.isLoopback()||intf.isVirtual()){// continue; } Enumeration inets=intf.getInetAddresses(); while(inets.hasMoreElements()){ InetAddressaddr=inets.nextElement(); if(addr.isLoopbackAddress()||!addr.isSiteLocalAddress()||addr.isAnyLocalAddress()){ continue; } list.add(addr.getHostAddress()); } } returnlist.toArray(newString[0]); } /** *判断操作系统是否是Windows * *@return */ publicstaticbooleanisWindowsOS(){ booleanisWindowsOS=false; StringosName=System.getProperty("os.name"); if(osName.toLowerCase().indexOf("windows")>-1){ isWindowsOS=true; } returnisWindowsOS; } publicstaticvoidmain(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(UnknownHostExceptione){ }catch(Exceptione){ e.printStackTrace(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。