Get the ip address of this machine (Windows+Lunix)

1.源码:
package com.jindian.spingbootecharts.controller;

import org.springframework.stereotype.Controller;

import java.net.*;
import java.util.Enumeration;

/**
 * @author tank
 * @Description:
 * @create 2019-04-11 9:33
 **/
@Controller
public class TestController {

    //获取本机的IP地址
    public static void main(String[] args) {
        //address1();
        try {
            final InetAddress localHostLANAddress = getLocalHostLANAddress();
            System.out.println("localHostLANAddress="+localHostLANAddress.getHostAddress());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Method 1: Can only be used under Windows 
    public static void address(){ 
        try { 
            final InetAddress localHost = InetAddress.getLocalHost(); 
            // final String hostAddress = InetAddress.getLocalHost().getHostAddress(); 
            System.out. println("The local address is: "+localHost); 
        } catch (UnknownHostException e) { 
            e.printStackTrace(); 
        } 
    } 

    //Method 2: Multiple values ​​will be generated under windows (inaccurate) 
    public static void address1() { 
        try{ 
        Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces(); 
        InetAddress ip = null; 
        while (allNetInterfaces.hasMoreElements()) 
        {
            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
            System.out.println(netInterface.getName());
            Enumeration addresses = netInterface.getInetAddresses();
            while (addresses.hasMoreElements())
            {
                ip = (InetAddress) addresses.nextElement();
                if (ip != null && ip instanceof Inet4Address)
                {
                    System.out.println("本机的IP = " + ip.getHostAddress());
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }

    }

    //Correct method
    public static InetAddress getLocalHostLANAddress() { 
        try { 
            InetAddress candidateAddress = null; 
            // Traverse all network interfaces 
            for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements(); ) { 
                NetworkInterface iface = (NetworkInterface) ifaces.nextElement( ); 
                // Traverse IP under all interfaces 
                for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements(); ) { 
                    InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); 
                    if (!inetAddr.isLoopbackAddress( )) {// Exclude loopback type address 
                        if (inetAddr.isSiteLocalAddress()) {
                            // If it is a site-local address, this is it 
                            return inetAddr; 
                        } else if (candidateAddress == null) { 
                            // The site-local address is not found, record the candidate address first 
                            candidateAddress = inetAddr; 
                        } 
                    } 
                } 
            } 
            if (candidateAddress != null) { 
                return candidateAddress; 
            } 
            // If the non-loopback address is not found, only the last-choice solution 
            InetAddress can be used jdkSuppliedAddress = InetAddress.getLocalHost(); 
            return jdkSuppliedAddress; 
        } catch (Exception e) {
            e.printStackTrace(); 
        } 
        return null; 
    } 


} 2. Local ip address:



 

Guess you like

Origin blog.csdn.net/Tank_666/article/details/90176149