• 请求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;
        }
    }
  • 相关阅读:
    Spring boot启动后没有生成日志文件问题排错
    keepalived 容器在宿主机重启后无法启动问题:报错:daemon is already running
    【转】iptables命令、规则、参数详解
    【转】VMwareCLI命令参考
    【转】通过ionice和nice降低shell脚本运行的优先级
    【转】dd命令详解及利用dd测试磁盘性能
    【转】Keepalived无法绑定VIP故障排查经历
    【转】浏览器Request Header和Response Header的内容
    【转】Spring Boot 日志配置(超详细)
    Spring数据访问和事务
  • 原文地址:https://www.cnblogs.com/huyanlon/p/10641188.html
Copyright © 2020-2023  润新知