获取用户登陆所在的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 ...
随机推荐
- Mysql基本代码操作
Mysql的基本代码生成操作 创建一个数据库 (myschool是数据库名) create database myschool; 删除数据库 drop database myschool 创建一个 ...
- CentOS6/7快捷使用gcc5
Centos6/7自带的gcc为4.x版本,可通过devtoolset工具集安装gcc5.x版本 1. 添加yum源 1)CentOS6 [hhorak-devtoolset--rebuild-boo ...
- Ansys热应力计算
目录 问题说明 温度场分析APDL 结果 问题说明 样块上下两端固定,在室温20℃下进行夹紧,分析其升温到150℃时的热应力. 采用间接法进行分析,温度场单元选择278,应力场单元为185 首先进行稳 ...
- JSON数据、PHP数组 转换 Excel表格
//excel输出 header("Content-type:application/vnd.ms-excel"); header("Content-Dispositio ...
- 左耳听风-ARTS-第1周
Algorithm https://leetcode.com/problems/longest-common-prefix/ class Solution { public String longes ...
- Django学习笔记之数据库-模型的操作
模型的操作 在ORM框架中,所有模型相关的操作,比如添加/删除等.其实都是映射到数据库中一条数据的操作.因此模型操作也就是数据库表中数据的操作. 添加模型 添加模型到数据库中.首先需要创建一个模型.创 ...
- DataFrame对行列的基本操作实战
1.pandas对行列的基本操作命令: import numpy as np import pandas as pd from pandas import Sereis, DataFrame ser ...
- SQL 快速生成不重复的卡号
--0042-9923-3598 select id = right('000000000000' + cast(cast(rand(checksum(newid()))*1000000000000 ...
- 小梵同学 GO!
刘德翠 1. Vue.js实战读书笔记(1) 2.Vue.js实战读书笔记--计算属性 3. Vue.js实战读书笔记--v-bind及class与style绑定 4. Vue.js实战读书笔记--内 ...
- 阿里云linux yum源配置
1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的CentOS-Base ...