Java利用Request請求如何獲取IP地址對應(yīng)的省份、城市詳解
前言
最近的一個項目中需要將不同省份的用戶,展示不同內(nèi)容,所以需要通過Request請求獲取IP地址, 然后通過IP獲取IP對應(yīng)省份。
這里的操作步驟一共有步:
1. 通過Request獲取IP
2. 通過IP獲取對應(yīng)省份、城市
3. 通過設(shè)置的省份和IP對應(yīng)省份進行比對,展示內(nèi)容
通過Request獲取IP
可以參考我的另外一篇文章【Java 通過Request請求獲取IP地址】下面是代碼:
public class IpAdrressUtil { /** * 獲取Ip地址 * @param request * @return */ private static String getIpAdrress(HttpServletRequest request) { String Xip = request.getHeader("X-Real-IP"); String XFor = request.getHeader("X-Forwarded-For"); if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){ //多次反向代理后會有多個ip值,第一個ip才是真實ip int index = XFor.indexOf(","); if(index != -1){ return XFor.substring(0,index); }else{ return XFor; } } XFor = Xip; if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){ return XFor; } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("Proxy-Client-IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("HTTP_CLIENT_IP"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) { XFor = request.getRemoteAddr(); } return XFor; } }
通過IP獲取對應(yīng)省份、城市
使用【GeoLite2 City】庫
目前開源的IP地址庫與城市對應(yīng)關(guān)系用的比較多的是MaxMind公司的GeoLite數(shù)據(jù)庫,GeoLite數(shù)據(jù)庫有開源版本和收費版本,我們使用的是開源版本,GeoLite目前已經(jīng)更新到2了,所以我們下載GeoLite2 City庫。
官方下載地址是【http://dev.maxmind.com/geoip/geoip2/geolite2/】
本地下載地址是【http://xiazai.jb51.net/201710/yuanma/GeoLite2-City_20171003(jb51.net).rar】
如果覺得慢就用迅雷下。下載完成后就是,下載完成就解壓。得到【GeoLite2-City.mmdb】文件,這個就是數(shù)據(jù)庫。
Java例子是這樣使用的:
首先在項目中加入maven支持
<dependency> <groupId>com.maxmind.geoip2</groupId> <artifactId>geoip2</artifactId> <version>2.8.1</version> </dependency>
然后通過GeoLite2查詢得到省份、城市:
public static void main(String[] args) throws Exception{ // 創(chuàng)建 GeoLite2 數(shù)據(jù)庫 File database = new File("/Users/admin/GeoLite2-City.mmdb"); // 讀取數(shù)據(jù)庫內(nèi)容 DatabaseReader reader = new DatabaseReader.Builder(database).build(); InetAddress ipAddress = InetAddress.getByName("171.108.233.157"); // 獲取查詢結(jié)果 CityResponse response = reader.city(ipAddress); // 獲取國家信息 Country country = response.getCountry(); System.out.println(country.getIsoCode()); // 'CN' System.out.println(country.getName()); // 'China' System.out.println(country.getNames().get("zh-CN")); // '中國' // 獲取省份 Subdivision subdivision = response.getMostSpecificSubdivision(); System.out.println(subdivision.getName()); // 'Guangxi Zhuangzu Zizhiqu' System.out.println(subdivision.getIsoCode()); // '45' System.out.println(subdivision.getNames().get("zh-CN")); // '廣西壯族自治區(qū)' // 獲取城市 City city = response.getCity(); System.out.println(city.getName()); // 'Nanning' Postal postal = response.getPostal(); System.out.println(postal.getCode()); // 'null' System.out.println(city.getNames().get("zh-CN")); // '南寧' Location location = response.getLocation(); System.out.println(location.getLatitude()); // 22.8167 System.out.println(location.getLongitude()); // 108.3167 }
如果是生產(chǎn)環(huán)境,可以直接創(chuàng)建一個Service,在Service初始化的時候創(chuàng)建reader對象,然后在公共方法中通過ip查詢地址,下面以省份為例:
import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.model.CityResponse; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.io.File; import java.net.InetAddress; /** * IP地址服務(wù) */ @Service public class IpAddressService { private static Logger logger = LoggerFactory.getLogger(IpAddressService.class); private static String dbPath = "/usr/local/GeoLite2-City.mmdb"; private static DatabaseReader reader; @Autowired private Environment env; @PostConstruct public void init() { try { String path = env.getProperty("geolite2.city.db.path"); if (StringUtils.isNotBlank(path)) { dbPath = path; } File database = new File(dbPath); reader = new DatabaseReader.Builder(database).build(); } catch (Exception e) { logger.error("IP地址服務(wù)初始化異常:" + e.getMessage(), e); } } public String getSubdivision(String ipAddress){ try { CityResponse response = reader.city(InetAddress.getByName(ipAddress)); return response.getMostSpecificSubdivision().getNames().get("zh-CN"); }catch (Exception e){ logger.error("根據(jù)IP[{}]獲取省份失敗:{}", ipAddress, e.getMessage()); return null; } } }
然后在需要的地方就行判斷:
String areaNames = {"北京","天津","上海"}; String subdivision = ipAddressService.getSubdivision(getIpAdrress(request)); // 匹配 if(containsArea(subdivision, areaNames)){ // 處理... }
匹配方法:
private boolean containsArea(String name, String[] areaNames) { if(StringUtils.isBlank(name)){ return false; } for(String areaName : areaNames){ if(name.contains(areaName)){ return true; } } return false; // 按地域返回數(shù)據(jù) }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Apache Commons fileUpload實現(xiàn)文件上傳之一
這篇文章主要介紹了Apache Commons fileUpload實現(xiàn)文件上傳之一的相關(guān)資料,需要的朋友可以參考下2016-03-03Mybatis之a(chǎn)ssociation和collection用法
這篇文章主要介紹了Mybatis之a(chǎn)ssociation和collection用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02解決idea services窗口不見的一種特殊情況(小白采坑系列)
這篇文章主要介紹了解決idea services窗口不見的一種特殊情況(小白采坑系列),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09SpringBoot2 整合Nacos組件及環(huán)境搭建和入門案例解析
這篇文章主要介紹了SpringBoot2 整合Nacos組件,環(huán)境搭建和入門案例詳解,在整合springboot2時注意版本 0.2.x.RELEASE 對應(yīng)的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 對應(yīng)的是 Spring Boot 1.x 版本,具體內(nèi)容詳情跟隨小編一起看看吧2022-03-03Lombok中@EqualsAndHashCode注解的使用及說明
這篇文章主要介紹了Lombok中@EqualsAndHashCode注解的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證問題
OpenCV是一個基于Apache2.0許可(開源)發(fā)行的跨平臺計算機視覺和機器學(xué)習(xí)軟件庫,可以運行在Linux、Windows、Android和Mac?OS操作系統(tǒng)上,這篇文章主要介紹了Java?+?Selenium?+?OpenCV解決自動化測試中的滑塊驗證,需要的朋友可以參考下2022-07-07