java代码实现外网IP的获取

来源:互联网 发布:你睡过几个男人 知乎 编辑:程序博客网 时间:2024/05/17 22:07
private static String getMyIP() throws IOException {        InputStream ins = null;        try {            String l = "http://1212.ip138.com/ic.asp";            URL url = new URL(l);            URLConnection con = url.openConnection();            ins = con.getInputStream();            InputStreamReader isReader = new InputStreamReader(ins, "gb2312");            BufferedReader bReader = new BufferedReader(isReader);            StringBuffer webContent = new StringBuffer();            String str = null;            while ((str = bReader.readLine()) != null) {                webContent.append(str);            }            System.out.println(webContent);            int start = webContent.indexOf("[") + 1;            int end = webContent.indexOf("]");            return webContent.substring(start, end);        } finally {            if (ins != null) {                ins.close();            }        }    }
原创粉丝点击