public static String getAddressByIp(String ip) {
String resout = "";
try {
if (isInner(ip) || "127.0.0.1".equals(ip)) {
resout = "内网IP:" + ip;
} else {
String str = getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip);
System.out.println(str);
JsonObject jsonObject = (JsonObject) new JsonParser().parse(str);
if (jsonObject != null) {
JsonElement data = jsonObject.get("data");
int code = jsonObject.get("code").getAsInt();
if (code == 0) {
resout = data.getAsJsonObject().get("country").getAsString()
+ data.getAsJsonObject().get("area").getAsString()
+ data.getAsJsonObject().get("city").getAsString()
+ data.getAsJsonObject().get("isp").getAsString();
} else {
resout = "未知";
}
}
} } catch (Exception e) {
e.printStackTrace();
resout = "未知";
}
System.out.println("result: " + resout);
return resout;
} public static String getJsonContent(String urlStr) {
String result = "";
try {
// 获取HttpURLConnection连接对象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// 设置连接属性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 获取相应码
int respCode = httpConn.getResponseCode();
if (respCode == 200) {
result = ConvertStream2Json(httpConn.getInputStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
} private static String ConvertStream2Json(InputStream inputStream) {
String jsonStr = "";
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
jsonStr = buffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return jsonStr;
}
private static boolean isInner(String ip) {
String reg = "(10|172|192)\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]{0,1})\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]{0,1})\\.([0-1][0-9]{0,2}|[2][0-5]{0,2}|[3-9][0-9]{0,1})";// 正则表达式=。
Pattern p = Pattern.compile(reg);
Matcher matcher = p.matcher(ip);
return matcher.find();
}

根据ip获取对应的省市区的更多相关文章

  1. 百度地图用ip获取当前位置的经纬度(高精度)

    步骤比较简单先上百度地图API官网,申请一个应用AK(访问凭据):查看一下高进度定位的API,看看是否都符合要求下面直接上代码 /** * 根据ip获取地理坐标 * @param ip * @retu ...

  2. 根据ip获取用户地理位置

    各大网站都提供根据ip获取用户地理位置信息,这里以新浪的接口为例子 接口地址为:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js ...

  3. C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市

    百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...

  4. PHP通过IP 获取 地理位置(实例)

    发布:JB02   来源:脚本学堂  分享一例php代码,实现通过IP地址获取访问者的地理位置,在php编程中经常用到,有需要的朋友参考下吧.本节内容:PHP通过IP获取地理位置 例子: 复制代码代码 ...

  5. PHP通过IP 获取 地理位置(实例代码)

    发布:JB02   来源:脚本学堂 分享一例php代码,实现通过IP地址获取访问者的地理位置,在php编程中经常用到,有需要的朋友参考下吧.本节内容:PHP通过IP获取地理位置 例子: 复制代码代码示 ...

  6. Android模拟器的ip获取以及模拟器之间socket通信

    Android模拟器的ip获取以及模拟器之间socket通信           http://kalogen.iteye.com/blog/1565507 作者:李波 实现网络五子棋时用到了两个设备 ...

  7. C# 根据IP获取省市

    /// <summary> /// 根据IP获取省市 /// </summary> public void GetAddressByIp() { string ip = &qu ...

  8. php根据IP获取经纬度信息--百度地图篇

    一.前言 之前一篇写过 php根据IP获取IP所在城市  ,但是还想再精确一点,获取这个IP所在的经纬度信息,该怎么办呢? 百度地图为我提供了一种解决方案(当然还有其他的解决方案). 先总的来数一下, ...

  9. 根据IP获取所在的国家城市

    根据IP获取所在的国家城市 新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:htt ...

随机推荐

  1. js实现小功能 动态赋值

  2. sql 书写 规范 优化

    规范 做注解  便于修改和优化  规范 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE map ...

  3. 在js中绑定onclick事件为什么不加括号,在html代码中必须要加?

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. photoshop编辑pdf文件

    对于PDF文件透明背景的问题 PDF文件背景是透明的,如何使其变成白色 怎样通过photoshop打开多页PDF,编辑后仍保存为多页 注意shift全选,"页面选项"处的'裁剪到' ...

  5. thymeleaf 页面获取当前页面的完整URL地址

    下面两种方法是一样的 <div th:text="${#httpServletRequest.getRequestURL() +'?'+ #httpServletRequest.get ...

  6. opencv 增强现实(二):特征点匹配

    import cv2 as cv import numpy as np # def draw_keypoints(img, keypoints): # for kp in keypoints: # x ...

  7. CQOI2018异或序列 [莫队]

    莫队板子 用于复习 #include <cstdio> #include <cstdlib> #include <algorithm> #include <c ...

  8. BZOJ4259残缺的字符串

    题目描述 很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同程度的残缺. ...

  9. c# 获取端口的连接数,网站的连接数

    端口连接数: public static int PortTcpConnection(int port) { IPGlobalProperties properti = IPGlobalPropert ...

  10. django - 总结 - cnblog

    1.头像预览 -------方法1-------- 点击头像------>点击input img和input重合; img在label,input-->display:none $(&qu ...