精华内容
下载资源
问答
  • Java获取IP地址

    2021-03-23 10:20:43
    Java获取IP地址
    import java.net.Inet4Address;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.util.Enumeration;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * @version v1.0
     * @Description: TODO
     * @Author: cnb
     * @Date: 2020/1/18 23:08
     */
    public class IpUtils {
    
        private static final String[] HEADERS = {
                "X-Forwarded-For",
                "Proxy-Client-IP",
                "WL-Proxy-Client-IP",
                "HTTP_X_FORWARDED_FOR",
                "HTTP_X_FORWARDED",
                "HTTP_X_CLUSTER_CLIENT_IP",
                "HTTP_CLIENT_IP",
                "HTTP_FORWARDED_FOR",
                "HTTP_FORWARDED",
                "HTTP_VIA",
                "REMOTE_ADDR",
                "X-Real-IP"
        };
    
        /**
         * 判断ip是否为空,空返回true
         * @param ip
         * @return
         */
        public static boolean isEmptyIp(final String ip){
            return (ip == null || ip.length() == 0 || ip.trim().equals("") || "unknown".equalsIgnoreCase(ip));
        }
    
    
        /**
         * 判断ip是否不为空,不为空返回true
         * @param ip
         * @return
         */
        public static boolean isNotEmptyIp(final String ip){
            return !isEmptyIp(ip);
        }
    
        /***
         * 获取客户端ip地址(可以穿透代理)
         * @param request HttpServletRequest
         * @return
         */
        public static String getIpAddress(HttpServletRequest request) {
            String ip = "";
            for (String header : HEADERS) {
                ip = request.getHeader(header);
                if(isNotEmptyIp(ip)) {
                    break;
                }
            }
            if(isEmptyIp(ip)){
                ip = request.getRemoteAddr();
            }
            if(isNotEmptyIp(ip) && ip.contains(",")){
                ip = ip.split(",")[0];
            }
            if("0:0:0:0:0:0:0:1".equals(ip)){
                ip = "127.0.0.1";
            }
            return ip;
        }
    
    
        /**
         * 获取本机的局域网ip地址,兼容Linux
         * @return String
         * @throws Exception
         */
        public String getLocalHostIP() throws Exception{
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            String localHostAddress = "";
            while(allNetInterfaces.hasMoreElements()){
                NetworkInterface networkInterface = allNetInterfaces.nextElement();
                Enumeration<InetAddress> address = networkInterface.getInetAddresses();
                while(address.hasMoreElements()){
                    InetAddress inetAddress = address.nextElement();
                    if(inetAddress != null && inetAddress instanceof Inet4Address){
                        localHostAddress = inetAddress.getHostAddress();
                    }
                }
            }
            return localHostAddress;
        }
        
    }
    
    

    关注我的技术公众号,每天都有优质技术文章推送。
    微信扫一扫下方二维码即可关注:
    在这里插入图片描述

    展开全文
  • java获取IP地址

    2016-02-01 15:44:57
    java获取IP地址。别人总结的文章,自己备份一下,需要的同学可以参考
  • Java获取ip地址

    2021-04-15 14:58:03
    Java获取ip地址 package com.sinosoft.ireport; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util....

    Java获取ip地址

    package com.sinosoft.ireport;
    
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Enumeration;
    import javax.servlet.http.HttpServletRequest;
    
    
    /**
     * @program: Report
     * @description: 获取IP地址
     * @author: XWJ
     * @create: 2021-04-14 21:03
     **/
    public class WebToolUtils {
    
        /**
         * 获取本地IP地址
         *
         * @throws SocketException
         */
        public static String getLocalIP() throws UnknownHostException, SocketException {
            if (isWindowsOS()) {
                return InetAddress.getLocalHost().getHostAddress();
            } else {
                return getLinuxLocalIp();
            }
        }
    
        /**
         * 判断操作系统是否是Windows
         *
         * @return
         */
        public static boolean isWindowsOS() {
            boolean isWindowsOS = false;
            String osName = System.getProperty("os.name");
            if (osName.toLowerCase().indexOf("windows") > -1) {
                isWindowsOS = true;
            }
            return isWindowsOS;
        }
    
        /**
         * 获取本地Host名称
         */
        public static String getLocalHostName() throws UnknownHostException {
            return InetAddress.getLocalHost().getHostName();
        }
    
        /**
         * 获取Linux下的IP地址
         *
         * @return IP地址
         * @throws SocketException
         */
        private static String getLinuxLocalIp() throws SocketException {
            String ip = "";
            try {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                    NetworkInterface intf = en.nextElement();
                    String name = intf.getName();
                    if (!name.contains("docker") && !name.contains("lo")) {
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress()) {
                                String ipaddress = inetAddress.getHostAddress().toString();
                                if (!ipaddress.contains("::") && !ipaddress.contains("0:0:") && !ipaddress.contains("fe80")) {
                                    ip = ipaddress;
                                    System.out.println(ipaddress);
                                }
                            }
                        }
                    }
                }
            } catch (SocketException ex) {
                System.out.println("获取ip地址异常");
                ip = "127.0.0.1";
                ex.printStackTrace();
            }
            System.out.println("IP:" + ip);
            return ip;
        }
    
        /**
         * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
         * <p>
         * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?
         * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
         * <p>
         * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130,
         * 192.168.1.100
         * <p>
         * 用户真实IP为: 192.168.1.110
         *
         * @param request
         * @return
         */
        public static String getIpAddress(HttpServletRequest request) {
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip;
        }
    
    }
    
    展开全文
  • java获取ip地址

    2012-10-26 23:09:00
    网上找的java获取ip地址的详细信息文档。
  • java 获取ip地址

    2021-05-10 16:14:12
    //获取服务器的IP地址 InetAddress address = getCurrentIp(); serverIp = address.getHostAddress(); } public static InetAddress getCurrentIp() { try { Enumeration<NetworkInterface> networ.
    public static String serverIp = null;
    static {
        //获取服务器的IP地址
        InetAddress address = getCurrentIp();
        serverIp = address.getHostAddress();
    }
    
    public static InetAddress getCurrentIp() {
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) networkInterfaces.nextElement();
                Enumeration<InetAddress> nias = ni.getInetAddresses();
                while (nias.hasMoreElements()) {
                    InetAddress ia = (InetAddress) nias.nextElement();
                    if (!ia.isLinkLocalAddress() && !ia.isLoopbackAddress() && ia instanceof Inet4Address) {
                        return ia;
                    }
                }
            }
        } catch (SocketException e) {
            log.info("获取服务器IP地址有误:{}",e.getStackTrace());
        }
        return null;
    }

     

    测试:

    System.out.println(serverIp);
    展开全文
  • java 获取IP地址

    2019-04-23 10:06:00
    package util; import java.net.InetAddress;import java.net.UnknownHostException; import javax.servlet.... public class CusAccessObjectUtil { /** * 获取用户真实IP地址,不使用request...

    package util;

    import java.net.InetAddress;
    import java.net.UnknownHostException;

    import javax.servlet.http.HttpServletRequest;

    public class CusAccessObjectUtil {

    /**
    * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
    * 参考文章: http://developer.51cto.com/art/201111/305181.htm
    *
    * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?
    * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
    *
    * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130,
    * 192.168.1.100
    *
    * 用户真实IP为: 192.168.1.110
    *
    * @param request
    * @return
    */
    public static String getIpAddress(HttpServletRequest request) {
    String ipAddress = request.getHeader("x-forwarded-for");
    if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    ipAddress = request.getHeader("Proxy-Client-IP");
    }
    if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    ipAddress = request.getHeader("WL-Proxy-Client-IP");
    }
    if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    ipAddress = request.getRemoteAddr();
    if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){
    //根据网卡取本机配置的IP
    InetAddress inet=null;
    try {
    inet = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    ipAddress= inet.getHostAddress();
    }
    }
    //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
    if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15
    if(ipAddress.indexOf(",")>0){
    ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
    }
    }
    return ipAddress;
    }


    public static String getLocalIP() throws UnknownHostException {
    String localIP = "";
    InetAddress addr = (InetAddress) InetAddress.getLocalHost();

    localIP = addr.getHostAddress().toString();
    return localIP;
    }


    }

    转载于:https://www.cnblogs.com/nomengfei/p/10754746.html

    展开全文
  • JAVA获取IP地址

    2018-07-30 10:57:24
     * 获取IP地址  *   * 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址  * 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非...
  • Java 获取IP地址

    2019-04-19 09:41:36
    * 获取Ip地址 * @param request * @return */ public static String getIpAdrress(HttpServletRequest request) { String ipAddress = request.getHeader("x-forwarded-for"); if(ipAddress =...
  • java获取IP地址的方法发布时间:2020-06-10 11:54:31来源:亿速云阅读:184作者:Leah这篇文章运用了实例代码展示java获取IP地址的方法,代码非常详细,可供感兴趣的小伙伴们参考借鉴,希望对大家有所帮助。...
  • Java 获取 ip 地址

    2019-09-25 14:35:45
    String ip = InetAddress.getLocalHost().getHostAddress().toString(); 转载于:https://www.cnblogs.com/oneMr/p/10101703.html

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 4,221
精华内容 1,688
关键字:

java获取ip地址

java 订阅