其他分享
首页 > 其他分享> > 请求ip获取工具类

请求ip获取工具类

作者:互联网

package com.panchan.m2.utils;

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

/**
 * 工具类获取请求ip
 * @author panchankeji.hu
 *
 */
public class ServerIPConfigUtil {
    public static String getLocalIPForJava() {
        StringBuilder sb = new StringBuilder();
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface intf = (NetworkInterface) en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
//                        if (!inetAddress.isLoopbackAddress()&& !inetAddress.isLinkLocalAddress()&& inetAddress.isSiteLocalAddress()) {
                    if (!inetAddress.isLoopbackAddress()) {
                        String ipAddress = inetAddress.getHostAddress().toString();
                        if (ipAddress.startsWith("192.168")) {
                            sb.append(ipAddress);
                        }
//                            sb.append("|");
                    }
                }
            }
        } catch (SocketException e) {
        }
        return sb.toString();
    }

    public static String getLocalRealIP() {
        StringBuilder sb = new StringBuilder();
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
                NetworkInterface intf = (NetworkInterface) en.nextElement();
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
//                        if (!inetAddress.isLoopbackAddress()&& !inetAddress.isLinkLocalAddress()&& inetAddress.isSiteLocalAddress()) {
                    if (!inetAddress.isLoopbackAddress()) {
                        String ipAddress = inetAddress.getHostAddress().toString();
                        sb.append(ipAddress);
//                            sb.append("|");
                    }
                }
            }
        } catch (SocketException e) {
        }
        return sb.toString();
    }


    /**
     * 获取服务器IP地址
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String getServerIp(){
        String SERVER_IP = null;
        try {
            InetAddress address = InetAddress.getLocalHost();
            SERVER_IP=address.getHostAddress();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return SERVER_IP;
    }
}

 

标签:en,请求,InetAddress,ip,NetworkInterface,获取,sb,inetAddress,String
来源: https://www.cnblogs.com/huyanlon/p/10641188.html