All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.t.util.common.IpUtil Maven / Gradle / Ivy

package cn.t.util.common;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class IpUtil {

    /**
     * 获取本地ip
     * @return xxx
     */
    public static String getLocalIp() {
        StringBuilder ip = new StringBuilder();
        try {
            for (Enumeration en = NetworkInterface
                .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface ni = en.nextElement();
                for (Enumeration enumIpAdd = ni
                    .getInetAddresses(); enumIpAdd.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAdd.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                        && !inetAddress.isLinkLocalAddress()
                        && inetAddress.isSiteLocalAddress()) {
                        ip.append(inetAddress.getHostAddress());
                    }
                }
            }
        } catch (SocketException ex) {
            ex.printStackTrace();
        }
        return ip.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy