java 获取windows/linux/mac ip地址

  |   0 评论   |   6,168 浏览

    /**
     * 获取本地IP地址
     *
     * @throws SocketException
     */
    public static String getLocalIP() throws UnknownHostException, SocketException {
    	if (isWindowsOS()) {
    		return InetAddress.getLocalHost().getHostAddress();
    	} else {
    		return getLinuxLocalIp();
    	}
    }
    
    /**
     * 判断操作系统是否是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;
    }
    
    /**
     * 获取本地Host名称
     */
    public static String getLocalHostName() throws UnknownHostException {
    	return InetAddress.getLocalHost().getHostName();
    }
    
    /**
     * 获取Linux下的IP地址
     *
     * @return IP地址
     * @throws SocketException
     */
    private static String getLinuxLocalIp() throws SocketException {
    	String ip = "";
    	try {
    		for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
    			NetworkInterface intf = en.nextElement();
    			String name = intf.getName();
    			if (!name.contains("docker") && !name.contains("lo")) {
    				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
    					InetAddress inetAddress = enumIpAddr.nextElement();
    					if (!inetAddress.isLoopbackAddress()) {
    						String ipaddress = inetAddress.getHostAddress().toString();
    						if (!ipaddress.contains("::") && !ipaddress.contains("0:0:")
    								&& !ipaddress.contains("fe80")) {
    							ip = ipaddress;
    							System.out.println(ipaddress);
    						}
    					}
    				}
    			}
    		}
    	} catch (SocketException ex) {
    		System.out.println("获取ip地址异常");
    		ip = "127.0.0.1";
    		ex.printStackTrace();
    	}
    	System.out.println("IP:" + ip);
    	return ip;
    }
    

    评论

    发表评论

    validate