根据IP获取具体城市名称及经纬度坐标

用到两款工具, geoip-api 这个可以根据IP获取城市以及经纬度 但是获取的城市是拼音不是汉字 导致山西和陕西的拼音相同 无法分辨,所以引入第二款工具ip2region 可以根据ip获取到具体的省市IP供应商名称 格式如:中国|0|上海|上海市|联通

1.pom依赖和所需要的数据文件GeoLiteCity-2013-01-18.dat【下载地址 https://dev.maxmind.com/geoip/geoip2/geolite2/】 与 ip2region.db【下载地址https://gitee.com/lionsoul/ip2region/tree/v1.6.1-release】 放到/resources目录下就行了


        
            com.maxmind.geoip
            geoip-api
            1.3.1
        
        
            eu.bitwalker
            UserAgentUtils
            1.21
        
        
        
            org.lionsoul
            ip2region
            1.7
        

2.工具类配置 放下util报下就行  IPUtil.java  GEOIpAndAgentUtils.java

package com.****.utils;/*
 *created by 丁郁非
 *date 2019/7/17
 */

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;

import java.io.File;
import java.lang.reflect.Method;

public class IPUtil {

    public static String getCityInfo(String ip) {

        //db
        String dbPath = IPUtil.class.getResource("/ip2region.db").getPath();

        File file = new File(dbPath);
        if (file.exists() == false) {
            System.out.println("Error: Invalid ip2region.db file");
        }

        //查询算法
        int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
        //DbSearcher.BINARY_ALGORITHM //Binary
        //DbSearcher.MEMORY_ALGORITYM //Memory
        try {
            DbConfig config = new DbConfig();
            DbSearcher searcher = new DbSearcher(config, dbPath);

            //define the method
            Method method = null;
            switch (algorithm) {
                case DbSearcher.BTREE_ALGORITHM:
                    method = searcher.getClass().getMethod("btreeSearch", String.class);
                    break;
                case DbSearcher.BINARY_ALGORITHM:
                    method = searcher.getClass().getMethod("binarySearch", String.class);
                    break;
                case DbSearcher.MEMORY_ALGORITYM:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
            }

            DataBlock dataBlock = null;
            if (Util.isIpAddress(ip) == false) {
                System.out.println("Error: Invalid ip address");
            }

            dataBlock = (DataBlock) method.invoke(searcher, ip);

            return dataBlock.getRegion();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    public static void main(String[] args) throws Exception {
        System.err.println(getCityInfo("220.248.12.158"));
    }
}
package com.****.utils;

import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.UserAgent;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GEOIpAndAgentUtils {
    private static LookupService lookupService = null;

    static {
        File file = new File("src/main/resources/GeoLiteCity-2013-01-18.dat");
        System.out.println(file.getAbsolutePath());
        try {
            lookupService = new LookupService(file, LookupService.GEOIP_MEMORY_CACHE);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static Map parseLocaltion(String ip) {
        Location location = lookupService.getLocation(ip);
       /* String city = location.city;*/
        HashMap locationMap = new HashMap<>();
        String cityInfo = IPUtil.getCityInfo(ip);
        String[] split = cityInfo.split("\\|");

        locationMap.put("city", split[2]);//只需要获取当前省或者直辖市
        locationMap.put("point", new Float[]{location.longitude, location.latitude});
        return locationMap;
    }

    public static String parseAgentInfo(String agentInfo) {
        UserAgent agent = UserAgent.parseUserAgentString(agentInfo);
        Browser browser = agent.getBrowser();
        return browser.getName() + "|"
                + browser.getBrowserType().getName() + "|"
                + agent.getBrowserVersion().getVersion() + "|"
                + agent.getOperatingSystem().getManufacturer() + "|"
                + agent.getOperatingSystem().getName();
    }
}

3.用法举例:

Map location=GEOIpAndAgentUtils.parseLocaltion("1.198.72.174")
String s=new ObjectMapper().writeValueAsString(location)
//s中包含了地理位置的所有信息

你可能感兴趣的:(java)