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及获取所属信息的更多相关文章

  1. 通过jquery 获取用户当前所在的城市名称和IP地址

    下面这段JS代码是通过jquery 结合新浪IP地址库和QQip地址库接口获取用户当前所在的城市(省份)名称. 用户当前IP地址等数据.其中当前IP是通过 QQip地址库接口获取,其他数据都是通过 新 ...

  2. ASP.NET获取用户端的真实IP

    ASP.NET获取用户端的真实IP在各种场景都能用到,但是用户网端变幻莫测情况众多,获取真实IP还真是不容易啊.下面分享个比较好一点的方法: 获取IP初始版本 /// <summary> ...

  3. UWP开发:获取用户当前所在的网络环境(WiFi、移动网络、LAN…)

    原文:UWP开发:获取用户当前所在的网络环境(WiFi.移动网络.LAN-) UWP开发:获取用户当前所在的网络环境: 在uwp开发中,有时候,我们需要判断用户所在的网络,是WiFi,还是移动网络,给 ...

  4. 用户登陆显示cpu、负载、内存信息

    #用户登陆显示cpu.负载.内存信息 #!/bin/bash # hostip=`ifconfig eth0 |awk -F" +|:" '/Bcast/{print $4}'` ...

  5. php中获取用户登陆的IP地址以及常规处理

    本文为原创,转载请注明!  在我们开发多站点业务网站中,经常需要获取客户端的ip地址来给用户推荐其所在地址的信息的业务,用php获取客户端的ip地址,我们一般用到的PHP内置方法是$_SERVER[' ...

  6. Vue之获取用户当前所在省市

    今天小编给大家带来的是使用Vue获取用户所在城市,Vue是很强大的,给大家准备好现成的插件供大家调用,下面的Demo小编使用的是百度API. 首先我们从百度平台申请百度地图的秘钥,申请成功后我们将&l ...

  7. 钉钉开发入门,微应用识别用户身份,获取用户免登授权码code,获取用户userid,获取用户详细信息

    最近有个需求,在钉钉内,点击微应用,获取用户身份,根据获取到的用户身份去企业内部的用户中心做校验,校验通过,相关子系统直接登陆; 就是在获取这个用户身份的时候,网上的资料七零八落的,找的人烦躁的很,所 ...

  8. 小程序使用wx.chooseAddress获取用户手机号码,微信chooseAddress接口获取用户收货信息

    通常用户在商城购买产品后,需要填写他的收货信息,方便我们发货,但是在手机上写字非常不方便,一个客户的收货信息包括:姓名,地址和手机号码这些内容全部填写的话,至少要写20个字. 地址 所以有些客户在手机 ...

  9. Oracle,Mysql 获取用户下所有表名,获取表所有的列名及数据类型

    Mysql 下面是mysql获取数据库所有表的语句 select table_name from information_schema.TABLES where TABLE_SCHEMA='Usern ...

随机推荐

  1. day-11函数的形参与实参

    形参与实参 参数介绍: 函数为什么要有参数:因为内部的函数体需要外部的数据 怎么定义函数的参数:在定义函数阶段,函数名后面()中来定义函数的参数 怎么使用函数的参数:在函数体中用定义的参数名直接使用 ...

  2. 使用 opendistro for elasticsearch 做为graylog的后端存储

    graylog 是一个很不错的日志分析.收集.报警平台,包好了丰富的插件,同时内部的架构设计很不错 input 组件很多,使用stream.pipeline可以方便的进行数据处理,可以同时3.0 对于 ...

  3. direnv 一个强大的环境变量管理工具

      direnv 是一个基于golang 编写的强大的环境变量管理工具,可以帮助我们简化环境变量管理,而且 支持的平台比较多. 基本使用 下载二进制软件包 https://github.com/dir ...

  4. 2018-2019-2 网络对抗技术 20165308 Exp1 PC平台逆向破解

    2018-2019-2 网络对抗技术 20165308 Exp1 PC平台逆向破解 NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP汇编指令:执行到NOP指令时,CPU仅仅当做一个 ...

  5. 原生JS实现选中的radio变为未选中

    需求如下,radio已经选中,再点击,取消选中状态. 效果如链接:演示地址 直接上代码: <!DOCTYPE html> <html> <head> <met ...

  6. el-popover 的显示或隐藏,要在拿到真实dom之后再做控制

    el-popover 的显示或隐藏,要在拿到真实dom之后再做控制

  7. mac 中 git 操作账号的保存与删除

    mac 系统中,运行命令:git config -l,输出中看到credential.helper=osxkeychain时,说明 git 密码保存在 Keychain 中. 右上角搜索框内搜索 gi ...

  8. [踩坑系列]URLEncode 中对 空格的编码有 “+”和“%20”两种

    URL中的空格有时候被编码成%20,有时候被编码成加号+,曾经迷糊过一段时间,后来查了下资料才搞明白. 一个URL的基本组成部分包括协议(scheme),域名,端口号,路径和查询字符串(路径参数和锚点 ...

  9. 捕获数据中的某个序列---verilog

    捕获数据中的某个序列---verilog 状态变化图 先是检测序列,每当接收到cmp_equal信号时跳转到下一个状态,等待另外一个cmp_equal信号到来. 代码: always @ * case ...

  10. python-实现3级菜单(作业课)

    #任务: #显示3级菜单 #1级菜单#显示 3个城市 => 1北京 2上海 3广州 #2级菜单 #显示 选择1 北京 => B1 B2 B3 #2级菜单 #显示 选择2 上海 => ...