• 根据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();
        }
  • 相关阅读:
    MicroStation VBA 操作提示
    MicroStation VBA 可视化界面
    VBA 操作数字
    MicroStation VBA基础
    C#问题
    C#复习⑨(附带C#参考答案仅限参考)
    C#复习⑧
    C#复习⑦
    C#复习⑥
    C#复习⑤
  • 原文地址:https://www.cnblogs.com/jiefu/p/10621718.html
Copyright © 2020-2023  润新知