注册 登录
  • 欢迎访问Sharezer Blog

获取公网ip

JAVA sharezer 365次浏览 已收录 0个评论
/**
 * 获取公网ip
 * @return
 */
public String getNetIp() {
    String ip = "";
    InputStream inStream = null;
    try {
        URL infoUrl = new URL("http://1212.ip138.com/ic.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, "gb2312"));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                //builder.append(line).append("\n");
            }
            inStream.close();
            int start = builder.indexOf("[");
            int end = builder.indexOf("]");
            ip = builder.substring(start + 1, end);
            return ip;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Sharezer , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明获取公网ip
喜欢 (0)
[liangshaoze@sina.cn]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址