ITPub博客

首页 > IT职业 > IT生活 > java根据访问IP来得出IP所在地

java根据访问IP来得出IP所在地

IT生活 作者:sunoct 时间:2021-09-09 09:05:34 0 删除 编辑

前几天想给自己的网站添加一个统计ip地址的功能,这个做好之后总觉得之后个IP地址也不好看,网上一般给出ip地址的时候也会给出一个ip地址的所在地,所以我就查找了下网上文章研究了下,话不多说,接下来开始正文

网上的方法大概就是根据淘宝或者新浪提供的接口,我们传入字符串,然后得到数据,我这里主要跟为两步

  • 根据接口传入ip地址
  • 得到json数据后进行解析
    先放接口,淘宝这个接口我进行了测试,是可以用的,返回json数据淘宝接口:http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串]
    主要的java代码
//根据IP地址返回地址描述
    private StringBuffer getIpDescr(String ipAddress){
        try
          {
            String strIP = ipAddress;
            URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + strIP); 
            URLConnection conn = url.openConnection(); 
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); 
            String line = null; 
            StringBuffer result = new StringBuffer(); 
            while((line = reader.readLine()) != null)
            { 
              result.append(line); 
            } 
            reader.close();         
            String jsonResult=result.toString();
            JSONObject jsonObject=JSONObject.fromObject(jsonResult);    
            JSONObject jsonObject1=(JSONObject)jsonObject.get("data");
            String country = jsonObject1.get("country").toString();
            String area =jsonObject1.get("area").toString();
            String region =jsonObject1.get("region").toString();
            String city =jsonObject1.get("city").toString();
            String isp =jsonObject1.get("isp").toString();
            StringBuffer descr = new StringBuffer();
            descr.append(country).append(":").append(area).append(":").append(region).append(":").append(city).append(":").append(isp);
            return descr;
          }
         catch( IOException e)
          { 
            logger.error("未找到", e);
            return null;
          }

    }

result直接就是返回的json数据了,但是返回的json数据中文为unicode编码,所有我又找了一个怎么将这里转化为中文
未进行解码的json数据是这样

{
    "code":0,
    "data":{
        "country":"u4e2du56fd",
        "country_id":"CN",
        "area":"u534eu4e1c",
        "area_id":"300000",
        "region":"u4e0au6d77u5e02",
        "region_id":"310000","city":
        "u4e0au6d77u5e02",
        "city_id":"310100",
        "county":"",
        "county_id":"-1",
        "isp":"u7535u4fe1",
        "isp_id":"100017",
        "ip":"101.226.65.104"
    }
}

中文都没有显示出来,我们需要这样的,

{
    "code": 0,
    "data": {
        "country": "中国",
        "country_id": "CN",
        "area": "华东",
        "area_id": "300000",
        "region": "上海市",
        "region_id": "310000",
        "city": "上海市",
        "city_id": "310100",
        "county": "",
        "county_id": "-1",
        "isp": "电信",
        "isp_id": "100017",
        "ip": "101.226.65.104"
    }
}

在程序中JSONObject 这里引入了一个包,我放上maven引入的格式文件

<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
    net.sf.json-libjson-lib2.4jdk15

值得注意的是这个包需要的是jdk1.5,不然会报错

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2280/viewspace-2799281/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    83
  • 访问量
    810