欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java利用ip2region實(shí)現(xiàn)獲取IP地址詳情

 更新時(shí)間:2022年07月29日 10:39:22   作者:有根頭發(fā)的程序猿  
ip2region是一個(gè)離線IP地址定位庫(kù)和IP定位數(shù)據(jù)管理框架,10微秒級(jí)別的查詢效率,提供了眾多主流編程語(yǔ)言的?xdb?數(shù)據(jù)生成和查詢客戶端實(shí)現(xiàn)。本文將利用ip2region實(shí)現(xiàn)獲取IP地址詳情,感興趣的可以了解一下

最近有個(gè)需求是通過(guò)ip地址獲取地址詳情,沒(méi)有弄過(guò)相關(guān)的接口,通過(guò)查資料搞定之后趕緊記錄分享一下

一開(kāi)始我是通過(guò)api的方法獲取但是總是報(bào)錯(cuò)獲取不到所以改用了ip2region離線ip解析的方法獲取的,廢話不多說(shuō)看操作。

首先要下載ip2region.db

下載地址:百度網(wǎng)盤(pán) 請(qǐng)輸入提取碼??????

提取碼:vik5

配置依賴

<dependency>
   <groupId>org.lionsoul</groupId>
   <artifactId>ip2region</artifactId>
   <version>1.7.2</version>
</dependency>

將文件放到resources目錄下

配置一個(gè)工具類(lèi)(里面有個(gè)測(cè)試方法)

之前我在網(wǎng)上查的資料用完之后本地是可以的測(cè)試的但是部署到服務(wù)器之后就找不到ip2region.db這個(gè)文件了,因?yàn)檫@個(gè)文件我是放在resources目錄 下面的,大家都知道打包之后resources這個(gè)目錄 是不存在的所以找不到這個(gè)文件,后來(lái)試了很久才搞定,下面是優(yōu)化的工具類(lèi)

@Slf4j
public class IpUtil {
 
    /**
     * 獲取IP地址
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-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.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        if (ip.split(",").length > 1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }
 
    /**
     * 根據(jù)IP地址獲取城市
     * @param ip
     * @return
     */
    public static String getCityInfo(String ip) throws IOException {
        // 這里能讀到這個(gè)流,但是是找不到這個(gè)文件的
        ClassPathResource classPathResource = new ClassPathResource("ip2region.db");
        // 我們新建一個(gè)文件,把流存放到這個(gè)文件,再?gòu)倪@個(gè)文件里面讀取數(shù)據(jù),就可以了
        File file = new File("ip2region.db");
        FileUtils.copyInputStreamToFile(classPathResource.getInputStream(),file);
        // todo 獲取輸入流
        InputStream inputStream = new FileInputStream(file);
        try {
            int len;
            byte[] buffer = new byte[1024];
            //todo 這里的輸出記得刪除再上線
            while ((len = inputStream.read(buffer)) != -1) {
               // System.out.println(new String(buffer, 0, len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // TODO 記得關(guān)閉流操作
            inputStream.close();
        }
        //查詢算法
        int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
        try {
            DbConfig config = new DbConfig();
            DbSearcher searcher = new DbSearcher(config,file.getPath());
            Method method;
            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;
                default:
                    return null;
            }
            DataBlock dataBlock;
            if (!Util.isIpAddress(ip)) {
                log.info("Error: Invalid ip address");
                return null;
            }
            dataBlock  = (DataBlock) method.invoke(searcher, ip);
            return dataBlock.getRegion();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static void main(String[] args) {
        try {
            // 這個(gè)ip的結(jié)果是   中國(guó)|0|香港|0|香港寬頻
            String detail = IpUtil.getCityInfo("58.176.81.30");
            System.out.println(detail);
        }catch (IOException e){
 
        }
 
    }
 
}

到這里就完事了。

這下面是一下國(guó)外的ip地址可以測(cè)試一下

  '35.187.132.16',
  '35.187.132.18',
  '49.35.162.6',
  '5.188.210.227',
  '64.233.173.10',
  '74.125.151.127',
  '74.125.212.219',
  '74.125.212.221',
  '95.184.60.224'

到此這篇關(guān)于Java利用ip2region實(shí)現(xiàn)獲取IP地址詳情的文章就介紹到這了,更多相關(guān)Java ip2region獲取IP地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論