获取用户登陆所在的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 ...
随机推荐
- 手机游戏引擎 Cocos
Cocos是全球最受欢迎的移动游戏开发解决方案,整合了Cocos 2d-x.Cocos 2d-js.Cocos Studio.Cocos Code IDE等框架及工具,无论您是开发新手还是行业资深人士 ...
- Actifio快照池(snapshot pool)空间占用说明
快照池是什么? 快照池是根据定义的SLA(Service Level Agreement)来保存应用数据各时间点的黄金副本空间. 快照池空间的消耗 快照池空间由三种不同类型的虚拟磁盘使用: Stagi ...
- Day 16 模块和包的导入
包的认识 包通过文件夹来管理一些列功能相近的模块 包:一系列模块的集合体 重点:包中一定有一个专门来管理包中所有模块的文件 包名:存放一系列模块的文件夹名字 包名(包对象)存放的是管理模块的那个文件地 ...
- markdown特殊符号语法
符号 说明 对应编码 & AND符号 & < 小于 < > 大于 > _ ...
- update_engine-FilesystemVerifierAction和PostinstallRunnerAction
在介绍完了DownloadAction之后,还剩下FilesystemVerifierAction和PostinstallRunnerAction,下面开始对其进行分析. FilesystemVeri ...
- sql调优2
今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...
- Day14全局变量与局部变量
列表的所有方法都可以用,如remove clear 为了避免容易出错,全局变量名用大写,局部变量名用小写 nonlocal指上一级的name
- ulimit -a
在linux中执行ulimit -a 即可查询linux相关的参数 用ulimit命令是可以修改这些配置的命令的格式:ulimit [-SHacdefilmnpqrstuvx] [limit] 中间的 ...
- JRockit检测Tomcat内存溢出JAVA内存泄漏问题
http://blog.csdn.net/liyanhui1001/article/details/8240473 公司的一个Java应用系统上线以来,基本每1天OutOfMemoryError: P ...
- 软件推荐----截图软件Snagit
截图软件,比较推荐使用Snagit,功能上所有截图软件有的他都有.Snagit编辑器有一个[库]功能,可以按日期以及应用程序对截图进行分类整理,最赞的是你可以把整个库备份导出,在新的电脑上进行导入,所 ...