根据请求的真实IP地址,获取请求的位置信息

首先,需要导入maven包



    org.lionsoul
    ip2region
    1.7.2

获取真实IP地址的方法:getRealIp.java

获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址

   /**
     * 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
     *
     * @param request
     * @version 1.0
     * @date 2019/5/14 14:51
     */
    public static String getRealIp(HttpServletRequest request) {
        // 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址

        String ip = request.getHeader("X-Forwarded-For");
        if (log.isInfoEnabled()) {
            log.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            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();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

根据IP获取位置信息的 getUserAreaIdFromRequest.java

    /**
     * 根据ip 获取用户的地区
     *
     * @param request
     * @date 2019/5/14 15:31
     */
    public static String getUserAreaIdFromRequest(HttpServletRequest request) {
        String realIp = GetRealIpUtil.getRealIp(request);
        log.info("==> 从request中获取的真实Ip地址 :{}", realIp);
        String areaName = null;
        if (StringUtils.isBlank(realIp)) {
            return areaName;
        }
        String ipInfo = GetRealIpUtil.getIpInfo(realIp);
        log.info("ipInfo---->:{}", ipInfo);
        if (StringUtils.isNotBlank(ipInfo)) {
            // 1:内网ip (0|0|0|0|内网IP|内网IP|16392)
            // 0|0|0|0|内网IP|内网IP|16392
            // 0|中国|0|北京|北京市|移动|183155
            // 0|中国|0|海南省|0|电信|153149
            // 0|中国|0|海南省|海口市|电信|153094
            String[] ipInfoArr = StringUtils.split(ipInfo, "|");
            if (ipInfoArr != null && ipInfoArr.length == 7) {
                String countryName = ipInfoArr[1];
                String provinceName = ipInfoArr[3];
                String cityName = ipInfoArr[4];
                if ("0".equals(countryName) && "内网IP".equals(provinceName) && "内网IP".equals(cityName)) {
                    areaName = "中国";
                } else {
                    if ("中国".equals(countryName)) {
                        //省份不为空
                        if (StringUtils.isNotBlank(provinceName)) {
                            // 去掉“省”字
                            if (provinceName.contains("省")) {
                                provinceName = provinceName.substring(0, provinceName.length() - 1);
                            }
                        } else {
                            if (StringUtils.isNotBlank(cityName)) {
                                // 去掉“市”字
                                cityName = cityName.substring(0, cityName.length() - 1);
                                Areas.Area area = Areas.instance.getAreaByName(cityName);
                            } else {
                                areaName = "中国";
                            }
                        }

                    }
                }
                log.info("从ip中获取的地区名称---> 国家:{}, 省份:{}, 城市:{}", countryName, provinceName, cityName);
            }
        }
        log.info("对应的地区id:{}", areaName);
        return areaName;
    }
private static String getIpInfo(String ip) {
        String ipInfo = null;
        try {
            DbConfig config = new DbConfig();
            InputStream inputStream = GetRealIpUtil.class.getClassLoader().getResourceAsStream("ip2region/ip2region.db");
            log.info("inputStream ---->:{}", inputStream);
            byte[] mybyte = IOUtils.toByteArray(inputStream);
            DbSearcher searcher = new DbSearcher(config, mybyte);
            DataBlock dataBlock = searcher.memorySearch(ip);
            ipInfo = dataBlock.toString();
            if (StringUtils.isNotBlank(ipInfo)) {
                ipInfo = new String(ipInfo.getBytes("gbk"), StandardCharsets.UTF_8);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ipInfo;
    }

ip2region/ip2region.db 可以自己生成,生成方法自行百度,也可以去下载已经生成号的。下载地址:https://download.csdn.net/download/zuihongyan518/11258365

你可能感兴趣的:(Java,根据ip获取位置信息,ip2region)