首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java根据ip地址获取城市地域信息

java根据ip地址获取城市地域信息

作者头像
码农笔录
发布2018-06-29 17:54:54
4.2K0
发布2018-06-29 17:54:54
举报
文章被收录于专栏:码农笔录码农笔录

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;
}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年04月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • java根据ip地址获取城市地域信息
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档