zoukankan      html  css  js  c++  java
  • 根据ip获取国家

    <!-- https://mvnrepository.com/artifact/com.maxmind.geoip/geoip-api -->
            <dependency>
                <groupId>com.maxmind.geoip</groupId>
                <artifactId>geoip-api</artifactId>
                <version>1.3.0</version>
            </dependency>

    Country_GeoIP.dat在博客文件管理列表中
    package com.onloon.website.analytics.common.utils;
    
    import java.io.IOException;
    import java.net.InetAddress;
    
    import com.maxmind.geoip.Location;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.core.io.ClassPathResource;
    
    import com.maxmind.geoip.Country;
    import com.maxmind.geoip.LookupService;
    import com.onloon.website.analytics.common.exception.BusinessException;
    
    public class GeoIpUtil {
        
        private static final String COUNTRY_DATA_PATH="/ipdb/Country_GeoIP.dat";
        private static final String CITY_DATA_PATH="/ipdb/GeoLiteCity.dat";
    
        private static  LookupService lookupService;
        private static  LookupService lookupCityService;
    
        /**
         * 查询指定ip的国家英文名称
         * 
         * @author pengfeng(彭锋)
         * @time 下午3:44:00  
         * @param ip
         * @return
         */
        public static String getCountryEnName(String ip) {
            if(lookupService==null) {
                load();
            }
            if(lookupService==null) {
                return null;
            }
            Country country=lookupService.getCountry(ip);
            if(country==null||StringUtils.isBlank(country.getName())) {
                return null;
            }
            return country.getName();
        }
    
        /**
         * 根据IP获取位置信息
         * @param ip
         * @return
         */
        public static Location getLocatinInfo(String ip){
            if(lookupCityService==null) {
                loadCity();
            }
            if(lookupCityService==null) {
                return null;
            }
            return lookupCityService.getLocation(ip);
        }
        
        //加载数据文件
        private static synchronized void  load() {
            if(lookupService!=null) {
                return;
            }
            ClassPathResource resource = new ClassPathResource(COUNTRY_DATA_PATH);
            try {
                lookupService=new LookupService(resource.getFile());
            } catch (IOException e) {
                throw new BusinessException();
            }
        }
    
        //加载数据文件
        private static synchronized void  loadCity() {
            if(lookupCityService!=null) {
                return;
            }
            ClassPathResource resource = new ClassPathResource(CITY_DATA_PATH);
            try {
                lookupCityService=new LookupService(resource.getFile());
            } catch (IOException e) {
                throw new BusinessException();
            }
        }
    }
  • 相关阅读:
    并发之CAS无锁技术
    dubbo-admin打包和zookper安装
    读书笔记<深入理解JVM>01 关于OutOfMemoryError 堆空间的溢出
    关于mybatis和spring复合pom的异常
    ElasticSearch入门一
    Niginx +Tomcat 集群搭建
    使用自定义线程池优化EchoServer
    使用线程池优化Echo模型
    获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址
    java中double和float精度丢失问题
  • 原文地址:https://www.cnblogs.com/wanhua-wu/p/9327334.html
Copyright © 2011-2022 走看看