java根據ip地址獲取城市地域信息
這裏提供兩個公開的接口,一個是阿里的,一個是新浪的
http://ip.taobao.com/service/getIpInfo.php?ip=123.139.94.139
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=218.192.3.42
接下來上代碼,我這裏用的是springboot自帶的RestTemplate,各位如果沒用到可以用HttpURLConnection。案例是在攔截器裏獲取ip,並查詢地址。如果內網測試的話,獲取到的是內網ip,通過內網穿透出去訪問,可以獲取你的公網出口ip,或者吧ip直接寫死。
@SpringBootApplication
public class LgmallRestApplication {
@Autowired
private RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate() {
return builder.build();
}
public static void main(String[] args) {
SpringApplication.run(LgmallRestApplication.class, args);
}
}
/**
* @Author: nelson
* @Description: 商品瀏覽記錄攔截器
* @Date: created in 2018/03/31/16:49
*/
public class BrowseItemInterceptor implements HandlerInterceptor {
@Autowired
private RestTemplate restTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理後會有多個ip值,第一個ip纔是真實ip
if( ip.indexOf(",")!=-1 ){
ip = ip.split(",")[0];
}
}
//新浪查詢失敗查詢阿里
String sina = restTemplate.getForObject("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={ip}", String.class,ip);
SinaIpVo sinaIpVo = new Gson().fromJson(sina, SinaIpVo.class);
if(sinaIpVo.getRet()!=-1){
System.out.println(sinaIpVo.getProvince());
System.out.println(sinaIpVo.getCity());
}else{
String object = restTemplate.getForObject("http://ip.taobao.com/service/getIpInfo.php?ip={ip}", String.class,ip);
IpVo ipVo = new Gson().fromJson(object, IpVo.class);
// XX表示內網
if(ipVo.getCode()==0 && !ipVo.getAddress().getRegion().equals("XX")){
System.out.println(ipVo.getAddress().getRegion());
System.out.println(ipVo.getAddress().getCity());
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
阿里返回結果封裝的vo,省去get、set方法,需要其他的屬性根據返回json自己擴展。
/**
* @Author: nelson
* @Description: get city by ip
* @Date: created in 2018/03/31/17:40
*/
public class IpVo implements Serializable{
private Integer code;
private Address address;
public class Address implements Serializable{
private String ip;
private String region;
private String city;
}
}
新浪返回結果封裝的vo,省去get、set方法,需要其他的屬性根據返回json自己擴展。
/**
* @Author: nelson
* @Description: get city by ip
* @Date: created in 2018/03/31/17:40
*/
public class SinaIpVo implements Serializable{
private Integer ret;
private String province;
private String city;
}
關注
如果有問題,請在下方評論,或者加羣討論 200909980
關注下方微信公衆號,可以及時獲取到各種技術的乾貨哦,如果你有想推薦的帖子,也可以聯繫我們的。