Java利用ip2region實現(xiàn)獲取IP地址詳情
最近有個需求是通過ip地址獲取地址詳情,沒有弄過相關(guān)的接口,通過查資料搞定之后趕緊記錄分享一下
一開始我是通過api的方法獲取但是總是報錯獲取不到所以改用了ip2region離線ip解析的方法獲取的,廢話不多說看操作。
首先要下載ip2region.db
提取碼:vik5
配置依賴
<dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>1.7.2</version> </dependency>
將文件放到resources目錄下
配置一個工具類(里面有個測試方法)
之前我在網(wǎng)上查的資料用完之后本地是可以的測試的但是部署到服務器之后就找不到ip2region.db這個文件了,因為這個文件我是放在resources目錄 下面的,大家都知道打包之后resources這個目錄 是不存在的所以找不到這個文件,后來試了很久才搞定,下面是優(yōu)化的工具類
@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 { // 這里能讀到這個流,但是是找不到這個文件的 ClassPathResource classPathResource = new ClassPathResource("ip2region.db"); // 我們新建一個文件,把流存放到這個文件,再從這個文件里面讀取數(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 { // 這個ip的結(jié)果是 中國|0|香港|0|香港寬頻 String detail = IpUtil.getCityInfo("58.176.81.30"); System.out.println(detail); }catch (IOException e){ } } }
到這里就完事了。
這下面是一下國外的ip地址可以測試一下
'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實現(xiàn)獲取IP地址詳情的文章就介紹到這了,更多相關(guān)Java ip2region獲取IP地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java toString方法重寫工具之ToStringBuilder案例詳解
這篇文章主要介紹了Java toString方法重寫工具之ToStringBuilder案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08解析rainbond以應用為中心的架構(gòu)設(shè)計原理
這篇文章主要為大家介紹了rainbond以應用為中心的架構(gòu)設(shè)計實現(xiàn)及原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-02-02Java 中橋接模式——對象結(jié)構(gòu)型模式的實例詳解
這篇文章主要介紹了Java 中橋接模式——對象結(jié)構(gòu)型模式的實例詳解的相關(guān)資料,希望通過本文大家能掌握這部分知識,需要的朋友可以參考下2017-09-09