根据ip获取对应的省市区

    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();
    }
posted @ 2019-03-29 15:43  知识追求者  阅读(6086)  评论(0编辑  收藏  举报