获取客户端请求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

你可能感兴趣的:(tcp/ip,java,网络)