获取用户登陆所在的ip及获取所属信息
package com.tcl.topsale.download.entity;
public class GeoLocation {
private String countryCode;
private String countryName;
private String region;
private String regionName;
private String city;
private String postalCode;
private String latitude;
private String longitude;
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegionName() {
return regionName;
}
public void setRegionName(String regionName) {
this.regionName = regionName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
@Override
public String toString() {
return "GeoLocation [countryCode=" + countryCode + ", countryName="
+ countryName + ", region=" + region + ", regionName="
+ regionName + ", city=" + city + ", postalCode=" + postalCode
+ ", latitude=" + latitude + ", longitude=" + longitude + "]";
}
}
package com.tcl.topsale.download.util; import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Subdivision;
import com.tcl.topsale.download.entity.GeoLocation;
import com.weihua.common.base.action.BaseAction; /**
*
* @author fzl
*根据用户登陆ip获取国家code
*/ public class GeoLocationUtil extends BaseAction{ /**
*
*/
private static final long serialVersionUID = 1L;
private DatabaseReader reader;
private static Logger logger = LoggerFactory.getLogger(GeoLocationUtil.class); public GeoLocationUtil() {
String dataFile = "com/tcl/topsale/download/util/GeoLite2-City.mmdb";
URL url = getClass().getClassLoader().getResource(dataFile); if (url == null) {
System.err.println("location database is not found - " + dataFile);
} else {
try {
File database = new File(url.getPath());
System.out.println(new DatabaseReader.Builder(database).build()+"***************");
reader = new DatabaseReader.Builder(database).build();
} catch (Exception e) {
e.printStackTrace();
}
}
} // public static void main(String[] args) {
// GeoLocationUtil glt = new GeoLocationUtil();
// String ip = "114.119.117.180";
// glt.getLocationFromRequest(ip);
// System.out.println(glt.getLocationFromRequest(ip));
// } /**
* 获取ip地址映射的国家
* @param ipAddress
* @return
*/
public GeoLocation getLocationFromRequest(String ip) {
GeoLocation location = getLocationV2(ip);
return location;
} /**
* 获取ip地址
* @param request
* @return
*/
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
logger.info("Ip from user agent: {}", ip);
// 多个反向代理IP时,取第一个
int commaOffset = ip.indexOf(',');
if (commaOffset < ) {
ip = ip.equals("0:0:0:0:0:0:0:1") ? "61.183.88.58" : ip;
return ip;
}
ip = ip.substring(, commaOffset); ip = ip.equals("0:0:0:0:0:0:0:1") ? "61.183.88.58" : ip; return ip;
} /**
* 获取ip地址映射的国家
* @param ipAddress
* @return
*/
private GeoLocation getLocationV2(String ipAddress) {
GeoLocation geoLocation = null;
if (null == reader) {
//System.err.println("location database is not found.");
logger.error("location database is not found.");
} else {
try {
geoLocation = new GeoLocation();
InetAddress ipAdd = InetAddress.getByName(ipAddress);
CityResponse response = reader.city(ipAdd); Country country = response.getCountry();
geoLocation.setCountryCode(country.getIsoCode());
geoLocation.setCountryName(country.getName()); Subdivision subdivision = response.getMostSpecificSubdivision();
geoLocation.setRegionName(subdivision.getName()); City city = response.getCity();
geoLocation.setCity(city.getName());
geoLocation.setPostalCode(response.getPostal().getCode());
geoLocation.setLatitude(String.valueOf(response.getLocation().getLatitude()));
geoLocation.setLongitude(String.valueOf(response.getLocation().getLongitude()));
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
} catch (GeoIp2Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
}
}
return geoLocation;
} // public static void main(String[] args) throws Exception{
// File database = new File("D:/定位/GeoLite2-City.mmdb");
// DatabaseReader reader = new DatabaseReader.Builder(database).build();
// InetAddress ipAddress = InetAddress.getByName("14.106.124.11");
// CityResponse response = reader.city(ipAddress);
//
// Country country = response.getCountry();
// System.out.println(country.getIsoCode()); // 'US'
// System.out.println(country.getName()); // 'United States'
// System.out.println(country.getNames().get("zh-CN")); // '美国'
//
// Subdivision subdivision = response.getMostSpecificSubdivision();
// System.out.println(subdivision.getName()); // 'Minnesota'
// System.out.println(subdivision.getIsoCode()); // 'MN'
//
// City city = response.getCity();
// System.out.println(city.getName()); // 'Minneapolis'
// System.out.println(city.getNames().get("zh-CN")); // 'Minneapolis'
// Postal postal = response.getPostal();
// System.out.println(postal.getCode()); // '55455'
// Location location = response.getLocation();
// System.out.println(location.getLatitude()); // 44.9733
// System.out.println(location.getLongitude()); // -93.2323
// } }
需要用到 :GeoLite2-City.mmdb,
geoip2-2.2.0.jar,jackson-databind-2.5.3.jar,google-http-client-1.20.0.jar,
maxmind-db-1.0.0.jar,jackson-annotations-2.5.0.jar,core-2.5.3.jar,jsr305-1.3.9.jar
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
package com.tcl.topsale.download.entity;
public class GeoLocation {private String countryCode;private String countryName;private String region;private String regionName;private String city;private String postalCode;private String latitude;private String longitude;public String getCountryCode() {return countryCode;}public void setCountryCode(String countryCode) {this.countryCode = countryCode;}public String getCountryName() {return countryName;}public void setCountryName(String countryName) {this.countryName = countryName;}public String getRegion() {return region;}public void setRegion(String region) {this.region = region;}public String getRegionName() {return regionName;}public void setRegionName(String regionName) {this.regionName = regionName;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getPostalCode() {return postalCode;}public void setPostalCode(String postalCode) {this.postalCode = postalCode;}public String getLatitude() {return latitude;}public void setLatitude(String latitude) {this.latitude = latitude;}public String getLongitude() {return longitude;}public void setLongitude(String longitude) {this.longitude = longitude;}
@Overridepublic String toString() {return "GeoLocation [countryCode=" + countryCode + ", countryName="+ countryName + ", region=" + region + ", regionName="+ regionName + ", city=" + city + ", postalCode=" + postalCode+ ", latitude=" + latitude + ", longitude=" + longitude + "]";}}
获取用户登陆所在的ip及获取所属信息的更多相关文章
- 通过jquery 获取用户当前所在的城市名称和IP地址
下面这段JS代码是通过jquery 结合新浪IP地址库和QQip地址库接口获取用户当前所在的城市(省份)名称. 用户当前IP地址等数据.其中当前IP是通过 QQip地址库接口获取,其他数据都是通过 新 ...
- ASP.NET获取用户端的真实IP
ASP.NET获取用户端的真实IP在各种场景都能用到,但是用户网端变幻莫测情况众多,获取真实IP还真是不容易啊.下面分享个比较好一点的方法: 获取IP初始版本 /// <summary> ...
- UWP开发:获取用户当前所在的网络环境(WiFi、移动网络、LAN…)
原文:UWP开发:获取用户当前所在的网络环境(WiFi.移动网络.LAN-) UWP开发:获取用户当前所在的网络环境: 在uwp开发中,有时候,我们需要判断用户所在的网络,是WiFi,还是移动网络,给 ...
- 用户登陆显示cpu、负载、内存信息
#用户登陆显示cpu.负载.内存信息 #!/bin/bash # hostip=`ifconfig eth0 |awk -F" +|:" '/Bcast/{print $4}'` ...
- php中获取用户登陆的IP地址以及常规处理
本文为原创,转载请注明! 在我们开发多站点业务网站中,经常需要获取客户端的ip地址来给用户推荐其所在地址的信息的业务,用php获取客户端的ip地址,我们一般用到的PHP内置方法是$_SERVER[' ...
- Vue之获取用户当前所在省市
今天小编给大家带来的是使用Vue获取用户所在城市,Vue是很强大的,给大家准备好现成的插件供大家调用,下面的Demo小编使用的是百度API. 首先我们从百度平台申请百度地图的秘钥,申请成功后我们将&l ...
- 钉钉开发入门,微应用识别用户身份,获取用户免登授权码code,获取用户userid,获取用户详细信息
最近有个需求,在钉钉内,点击微应用,获取用户身份,根据获取到的用户身份去企业内部的用户中心做校验,校验通过,相关子系统直接登陆; 就是在获取这个用户身份的时候,网上的资料七零八落的,找的人烦躁的很,所 ...
- 小程序使用wx.chooseAddress获取用户手机号码,微信chooseAddress接口获取用户收货信息
通常用户在商城购买产品后,需要填写他的收货信息,方便我们发货,但是在手机上写字非常不方便,一个客户的收货信息包括:姓名,地址和手机号码这些内容全部填写的话,至少要写20个字. 地址 所以有些客户在手机 ...
- Oracle,Mysql 获取用户下所有表名,获取表所有的列名及数据类型
Mysql 下面是mysql获取数据库所有表的语句 select table_name from information_schema.TABLES where TABLE_SCHEMA='Usern ...
随机推荐
- Demystify Git
Git虽然很时髦,但是不好学,不好用. 关键是不好理解. 本文用最通俗易懂又直观的方式,帮助大家理解git, git的内部组织, 基本概念,还有常用 的工作流程. 本文来自墙外. 习某某说:" ...
- HNOI2006公路修建问题
https://www.luogu.org/problemnew/show/P2323 [题目描述] OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发 ...
- Kettle 部署
下载kettle包 访问https://community.hitachivantara.com/docs/DOC-1009855下载kettle包 选择想要的版本 下载zip包 解压kettle包 ...
- h5需要的浏览器插件
google浏览器插件: web前段助手.vue-tools.草料二维码
- aspnetcore 日志 serilog-aspnetcore
serilog-aspnetcore https://github.com/serilog/serilog-aspnetcore public static void Main(string[] ar ...
- python3-基础8
模块与包 什么是模块 模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. #在python中,模块的使用方式都是一样的,但其实细说的话,模块可以分为四个通用类别: 1 ...
- mybatis的逆向工程和中文注解
由于MyBatis Generator自带了生成注释的功能,但是,是英文的而且生成的根本无法理解,所以可以通过,修改他的源码来实现生成中文的注释,具体方式有以下几种: 1) 自定义CommentGen ...
- C# 6.0:Expression – Bodied Methods
Expression-bodied 方法是C# 6.0 中另一个能简化代码的特性.我们已经对lambda表达式将funciton和delegation关联起来的这种用法很熟悉了.Expression- ...
- P1041 传染病控制(dfs)
P1041 传染病控制 题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染 ...
- android studio Authentication failed for
今天更新项目代码提示 Authentication failed for 后来一起是把git平台密码修改了 忘了修改android studio 密码所以更新失败 我在android studi ...