关闭

Java 获得局域网IP与公网IP

标签: java
160人阅读 评论(0) 收藏 举报
分类:
/**获得局域网ip*/
public static String getMyIP() throws IOException {
        InputStream ins = null;
        try {
            URL url = new URL("http://iframe.ip138.com/ic.asp");
            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);
            }
            int start = webContent.indexOf("[") + 1;
            int end = webContent.indexOf("]");
            return webContent.substring(start, end);
        } finally {
            if (ins != null) {
                ins.close();
            }
        }
    }
    /**获得公网ip*/
    private static String getMyIPLocal() throws IOException {
        InetAddress ia = InetAddress.getLocalHost();
        return ia.getHostAddress();
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:413664次
    • 积分:8330
    • 等级:
    • 排名:第2465名
    • 原创:467篇
    • 转载:24篇
    • 译文:0篇
    • 评论:67条
    最新评论