zoukankan      html  css  js  c++  java
  • 根据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();
        }
  • 相关阅读:
    Linux文件与目录管理(一)
    Linux文件基本属性
    软工实践总结
    微软必应词典的调查与研究
    调研安卓开发环境的发展演变
    软件工程的预定目标
    学习进度第5周
    机械学习----KNN算法
    MyBatis:简介、第一个程序01(纯小白非常实用)
    解决数据库连接时区的问题
  • 原文地址:https://www.cnblogs.com/jiefu/p/10621718.html
Copyright © 2011-2022 走看看