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
新浪返回json
接下来上代码,我这里用的是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
关注下方微信公众号,可以及时获取到各种技术的干货哦,如果你有想推荐的帖子,也可以联系我们的。
码农笔录二维码
网友评论