• 获取客户端请求IP及IP所属城市


    添加pom依赖

         
                org.lionsoul
                ip2region
                2.6.5
           

    public class IpUtil {
        private static Searcher searcher;
        private static final String DEFAULT_UNKNOWN="unknown";
        private static final int IP_MIN_LENGTH=0;
        static{
            try {
                ResponseEntity entity= buildResponseEntity("ip2region.xdb");
                searcher = Searcher.newWithBuffer(entity.getBody());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static String getIpAddress(HttpServletRequest request) {
            String ip = DEFAULT_UNKNOWN;
            try {
                ip = request.getHeader("X-Forwarded-For");
                if (ip == null || ip.length() == IP_MIN_LENGTH || DEFAULT_UNKNOWN.equalsIgnoreCase(ip)) {
                    ip = request.getHeader("WL-Proxy-Client-IP");
                }
                if (ip == null || ip.length() == IP_MIN_LENGTH || DEFAULT_UNKNOWN.equalsIgnoreCase(ip)) {
                    ip = request.getHeader("Proxy-Client-IP");
                }
                if (ip == null || ip.length() == IP_MIN_LENGTH || DEFAULT_UNKNOWN.equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_CLIENT_IP");
                }
                if (ip == null || ip.length() == IP_MIN_LENGTH || DEFAULT_UNKNOWN.equalsIgnoreCase(ip)) {
                    ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                }
                if (ip == null || ip.length() == IP_MIN_LENGTH || DEFAULT_UNKNOWN.equalsIgnoreCase(ip)) {
                    ip = request.getRemoteAddr();
                }
                // 处理多级代理情况
                if (StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
                    int index = ip.indexOf(",");
                    if (index > 0) {
                        return ip.substring(0, index);
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return ip;
        }

        public static String getCityByIp(String ip) {
            if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
                return "局域网";
            }

            if (searcher == null) {
                return "...";
            }
            String region = null;
            String errorMessage = null;
            try {
                region = searcher.search(ip);
            } catch (Exception e) {
                errorMessage = e.getMessage();
                if (errorMessage != null && errorMessage.length() > 256) {
                    errorMessage = errorMessage.substring(0, 256);
                }
                e.printStackTrace();
            }
            return region;
        }

        public static ResponseEntity buildResponseEntity(String templateName) throws IOException {
            ClassPathResource classPathResource = new ClassPathResource(templateName);
            String filename = classPathResource.getFilename();
            @Cleanup InputStream inputStream = classPathResource.getInputStream();
            byte[] bytes = FileCopyUtils.copyToByteArray(inputStream);
            // 解决中文乱码问题
            String fileName = new String(filename.getBytes("UTF-8"), "iso-8859-1");
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", fileName);
            return new ResponseEntity(bytes, headers, HttpStatus.CREATED);
        }

        public static void main(String[] args){
            System.out.println("********/"+ getCityByIp("61.154.231.236"));
        }
    }

    完整代码:https://download.csdn.net/download/paj123456789/88478095

  • 相关阅读:
    从200W超级闪充看iQOO 10 Pro的 “共情”能量
    JavaScript 输出
    36-Jenkins-Job迁移
    HttpClient基本使用
    苹果手机H5 video标签播放视频问题以及.mov格式处理方案
    自定义 View 可以播放一段视频
    ER 图是什么
    Vue Router
    django-admin登录窗口添加验证码功能-(替换原有的login.html)captcha插件
    知识图谱补全(KGC)论文阅读笔记
  • 原文地址:https://blog.csdn.net/paj123456789/article/details/134072742