zoukankan      html  css  js  c++  java
  • 获取公网ip

    通过www.ip138.com网站获取的。

    ed2k://|file|%E7%B2%89%E8%A1%A3%E7%94%B5%E6%A2%AF.avi|1575469056|C00B1D70E8256231F7C4A856D591D73C|/

    View Code
    /**
         * 获取本机公网IP
         * @return
         */
        public static String getPublicIpAddress() {
            URL infoUrl = null;
            InputStream inStream = null;
            try {
                infoUrl = new URL("http://city.ip138.com/city.asp");
                URLConnection connection = infoUrl.openConnection();
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                int responseCode = httpConnection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    inStream = httpConnection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
                    StringBuilder strber = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        strber.append(line + "\n");
                    }
                    inStream.close();
                    if (!TextUtils.isEmpty(strber)) {
                        // 从反馈的结果中提取出IP地址
                        int start = strber.indexOf("[");
                        int end = strber.indexOf("]", start + 1);
                        line = strber.substring(start + 1, end);
                        return line;
                    }
                    return null;
                }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
  • 相关阅读:
    Alpha版本发布说明
    团队作业week9
    Alpha版本发布时间安排
    2014年团队博客
    本周团队分数统计
    当前个人及团队分数统计
    团队作业week7
    Java FX入门
    Java第06次实验提纲(集合)
    Java第04次实验提纲(面向对象2-继承、多态、抽象类与接口)
  • 原文地址:https://www.cnblogs.com/zhangyulogin/p/2795098.html
Copyright © 2011-2022 走看看