zoukankan      html  css  js  c++  java
  • 请求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;
        }
    }
  • 相关阅读:
    Python
    算法的时间复杂度和空间复杂度-总结
    列表自动滚动
    React 结合ant-mobile 省市区级联
    5G从小就梦想着自己要迎娶:高速率、低时延、大容量三个老婆
    一文读懂GaussDB(for Mongo)的计算存储分离架构
    cpu占用过高排查
    我是如何从零开始自学转行IT并进入世界500强实现薪资翻倍?
    Linux重定向用法详解
    说出来也许你不信,我被 Linux 终端嘲笑了……
  • 原文地址:https://www.cnblogs.com/huyanlon/p/10641188.html
Copyright © 2011-2022 走看看