码迷,mamicode.com
首页 > 其他好文 > 详细

获取本机外网ip地址

时间:2015-08-19 19:32:24      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

package com.ning;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;

public class Listip {

   public static void main(String[] args) throws Exception {   

    System.out.println("本机的外网IP是:"   

    //+ Listip.getWebIp("http://iframe.ip138.com/ic.asp"));  

     //+ Listip.getWebIp("http://www.ip138.com"));   

    + Listip.getWebIp("http://1111.ip138.com/ic.asp"));

   }  

  public static String getWebIp(String strUrl) {   

    try {    

      URL url = new URL(strUrl);    

      BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"GB2312"));   

       String s = "";   

       StringBuffer sb = new StringBuffer("");    

      String webContent = "";   

       while ((s = br.readLine()) != null) {     

        sb.append(s + "rn");   

       }   

       br.close();    

      webContent = sb.toString();    

      int start = webContent.indexOf("[") + 1;    

      int end = webContent.indexOf("]");    

      System.out.println("webContent=" + webContent);   

       System.out.println("start=" + start);    

      System.out.println("end=" + end);   

       if (start < 0 || end < 0) {     

        return null;     

      }   

       webContent = webContent.substring(start, end);   

       return webContent;   

    } catch (Exception e) {    

      e.printStackTrace();    

      return "error open url:" + strUrl;   

    }

 }

}

获取本机外网ip地址

标签:

原文地址:http://www.cnblogs.com/ning2015/p/4742849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!