获取公网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;
    }
原文地址:https://www.cnblogs.com/zhangyulogin/p/2795098.html