获取用户登陆所在的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 ...
随机推荐
- Salesforce Lightning Builder Flows (Salesforce Lightning 构建Flows)
本文构建Salesforce Lightning Flows 只是一个简单的实现步骤,原文地址如下: https://trailhead.salesforce.com/en/content/learn ...
- 面试题-Python高级
元类 Python 中类方法.类实例方法.静态方法有何区别? 类方法:是类对象的方法,在定义时需要在上方使用“@classmethod”进行装饰,形参为cls, 表示类对象,类对象和实例对象都可调用: ...
- Cent os6.5 安装python3.2
1.CentOS6.5 安装Python 的依赖包 yum groupinstall "Development tools" yum install zlib-devel bzip ...
- ActiveMQ (二)—发布订阅模式
ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...
- js中this最简单清晰的解释
引自 https://www.cnblogs.com/huangwentian/p/6854472.html#commentform ① this指向的,永远只可能是对象! ② this ...
- ajax高级操作
$('#ajax_submit').click(function () { $.ajax({ 'url':'/app_ajax', 'type':'post', 'data':$("#f1& ...
- 20164310Exp2后门原理与实践
一.基础问题回答 1.例举你能想到的一个后门进入到你系统中的可能方式 答:在莫名其妙的网站下载某些莫名奇妙的播放器. 2.例举你知道的后门如何启动起来(win及linux)的方式? 答:对于windo ...
- 64 位 Windows 平台开发注意要点之注册表重定向
Window 系统错误代码 ERROR_SUCCESS,本博客中一律使用 NO_ERROR 代替.虽然 ERROR_SUCCESS 与 NO_ERROR 是完全等价的,都代表成功,但是后者却和其他错误 ...
- Avalon MM 总线
对于Avalon Master来讲,Address信号代表一个字节(8-bit)的地址.Address的值必须与字节的宽度对齐,如果要对某个word的byte写,需要首先使用byteenable信号: ...
- WPF常用TriggerAction用法 (一)
Microsoft.Expression.Interactivity 常用TriggerAction-> CallMethodAction ChangePropertyAction Contro ...