JAVA根據(jù)ip地址獲取歸屬地的實(shí)現(xiàn)方法
IP獲取歸屬地
1.通過地址庫獲取
如果使用API接口獲取
,可能會(huì)出現(xiàn)服務(wù)掛了,或者服務(wù)地址不提供服務(wù)了等問題。而采用本地地址庫就沒有這些問題。
本文采用離線IP
地址定位庫 Ip2region,Ip2region
是一個(gè)離線IP
地址定位庫,微秒的查詢時(shí)間:
實(shí)現(xiàn)步驟:
訪問官網(wǎng)github地址:https://github.com/lionsoul2014/ip2region
找到data目錄下的:ip2region.xdb文件下載下來
把ip2region.xdb文件放在resources目錄下
在模塊中引入maven依賴
<dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>2.6.5</version> </dependency>
獲取歸屬地:
private Searcher searcher; @Override public String getIpAddress(String ip){ if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) { return "|||局域網(wǎng)ip"; } if (searcher == null) { try { File file = ResourceUtils.getFile("classpath:db/data_ip2region.xdb"); String dbPath = file.getPath(); searcher = Searcher.newWithFileOnly(dbPath); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 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(); } // 輸出 region System.out.println(region); return region; }
這里Searcher引用的是倉庫里面的檢索方法,到這里就完成了可以獲取到ip對(duì)應(yīng)的歸屬地。
調(diào)用方法后運(yùn)行結(jié)果如下
注意?。。。?本地是沒問題的 如果打成jar包放在linux服務(wù)器上會(huì)讀取不到
解決辦法:
public String getIpCity(String ip) { if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) { return "|||局域網(wǎng)ip"; } if (searcher == null) { try { //本地環(huán)境需要加上 classpath: // File file = ResourceUtils.getFile("db/data_ip2region.xdb"); // String dbPath = file.getPath(); // searcher = Searcher.newWithFileOnly(dbPath); //這里通過流獲取 解決jar包無法讀取文件問題 ResponseEntity<byte[]> test = test("db/data_ip2region.xdb"); searcher = Searcher.newWithBuffer(test.getBody()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 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(); } // 輸出 region return region; } public static ResponseEntity<byte[]> test(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); }
至于什么是Ip2region 官網(wǎng)上面有介紹這里就不多介紹了
到此這篇關(guān)于JAVA根據(jù)ip地址獲取歸屬地的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)JAVA ip地址獲取歸屬地內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud 的 Hystrix.功能及實(shí)踐詳解
這篇文章主要介紹了Spring Cloud 的 Hystrix.功能及實(shí)踐詳解,Hystrix 具備服務(wù)降級(jí)、服務(wù)熔斷、線程和信號(hào)隔離、請(qǐng)求緩存、請(qǐng)求合并以及服務(wù)監(jiān)控等強(qiáng)大功能,需要的朋友可以參考下2019-07-07Java類之間的關(guān)系圖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
在Java以及其他的面向?qū)ο笤O(shè)計(jì)模式中,類與類之間主要有6種關(guān)系,他們分別是:依賴、關(guān)聯(lián)、聚合、組合、繼承、實(shí)現(xiàn)。他們的耦合度依次增強(qiáng),有興趣的可以了解一下2017-08-08Apache?Commons?BeanUtils:?JavaBean操作方法
這篇文章主要介紹了Apache?Commons?BeanUtils:?JavaBean操作的藝術(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12springboot中使用ConstraintValidatorContext驗(yàn)證兩個(gè)字段內(nèi)容相同
開發(fā)修改密碼功能時(shí),通過ConstraintValidator校驗(yàn)新密碼和確認(rèn)新密碼的一致性,首先定義Matches注解和DTO對(duì)象,然后創(chuàng)建MatchesValidator類實(shí)現(xiàn)驗(yàn)證邏輯,對(duì)springboot驗(yàn)證字段內(nèi)容相同問題感興趣的朋友一起看看吧2024-10-10Java Spring-Cache key配置注意事項(xiàng)介紹
本文主要對(duì)java spring-cache key配置注意事項(xiàng)進(jìn)行了介紹,小編覺得還是挺不錯(cuò)的,這里分享給大家,供需要的朋友參考。2017-10-10IDEA運(yùn)行SpringBoot項(xiàng)目的超詳細(xì)步驟截圖
在當(dāng)前的開發(fā)中Spring Boot開發(fā)框架已經(jīng)成為主流,下面這篇文章主要給大家介紹了關(guān)于IDEA運(yùn)行SpringBoot項(xiàng)目的超詳細(xì)步驟截圖,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11