java获取本机外网IP

/**
     * 获取外网IP
     * @return
     * @throws IOException
     */
    public static String getMyIP() throws IOException {
        String url = "http://ip.chinaz.com/getip.aspx";
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

            StringBuilder sb = new StringBuilder();
            int cp;
            while ((cp = rd.read()) != -1) {
                sb.append((char) cp);
            }
            String jsonText = sb.toString();
            jsonText = jsonText.replaceAll("'", "");
            jsonText = jsonText.substring(1, jsonText.length() - 1);
            jsonText = jsonText.replaceAll(",", "<br/>");
            if(jsonText.indexOf("<br/>")>0){
                jsonText = jsonText.substring(3, jsonText.indexOf("<br/>"));
            }
            return jsonText;
        } finally {
            is.close();
           
        }
    }

猜你喜欢

转载自blog.csdn.net/u010539006/article/details/81222683