转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/

/**

* 获取外网IP、归属地、操作系统

* @return

*/

public static String[] getIp(){

String[] obj = new String[3];

StringBuffer strForeignIP = new StringBuffer("");

StringBuffer strLocation = new StringBuffer("");

StringBuffer strOperatorMessageation = new StringBuffer("");

String ipAddress = "";

String ipLocation = "";

String ipSystem = "";

StringBuffer strUrl =new StringBuffer("http://www.cz88.net/ip/viewip778.aspx");

try{

URL url = new URL(strUrl.toString());

URLConnection context = url.openConnection();

InputStream in = context.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(in, "gb2312"));//防止读出来的是乱码



String s = "";

StringBuffer sb = new StringBuffer("");

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

sb.append(s + "\r\n");

}

br.close();

String webContent = sb.toString();

if (null!=webContent && webContent.trim().length()>0){

String flagofForeignIPString = "IPMessage";

int startIP = webContent.indexOf(flagofForeignIPString) + flagofForeignIPString.length() + 2;

int endIP = webContent.indexOf("</span>", startIP);

strForeignIP.delete(0, webContent.length());

strForeignIP.append(webContent.substring(startIP, endIP));



String flagofLocationString = "AddrMessage";

int startLoc = webContent.indexOf(flagofLocationString)+ flagofLocationString.length() + 2;

int endLoc = webContent.indexOf("</span>", startLoc);

strLocation.delete(0, webContent.length());

strLocation.append(webContent.substring(startLoc, endLoc));



String flagoOperatorMessage = "OperatorMessage";

int startOpera = webContent.indexOf(flagoOperatorMessage)+ flagoOperatorMessage.length() + 2;

int endOpera = webContent.indexOf("</span>", startOpera);

strOperatorMessageation.delete(0, webContent.length());

strOperatorMessageation.append(webContent.substring(startOpera, endOpera));

}

}catch(Exception e){

System.out.println("IpUtil:get ip is failed:"+e.getMessage());

e.printStackTrace();

}

if(strForeignIP.toString().trim().length()<0){

ipAddress = "未知";

}else{

ipAddress = strForeignIP.toString();

}



if(strForeignIP.toString().trim().length()<0){

ipLocation = "未知";

}else{

ipLocation = strLocation.toString();

}



if(strForeignIP.toString().trim().length()<0){

ipSystem = "未知";

}else{

ipSystem = strOperatorMessageation.toString();

}

obj[0]=ipAddress;

obj[1]=ipLocation;

obj[2]=ipSystem;

return obj;

}

java获取外网ip地址的更多相关文章

  1. c#获取外网IP地址的方法

    1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP 2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP //获取宽带连接(PPPOE拨号)的IP地址,timeout超 ...

  2. C# 获取外网IP地址

    很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...

  3. MFC C++ 获取外网IP地址

    #include <afxinet.h> //GB2312 转换成 Unicode wchar_t* GB2312ToUnicode(const char* szGBString) { U ...

  4. linux 获取外网ip地址

    curl ifconfig.me 私有ip地址,获取公网ip

  5. android 根据网络来获取外网ip地址及国家,地区的接口

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...

  6. C# Winform程序获取外网IP地址

    string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); ...

  7. 获取外网IP地址

    public static string GetRealIP(){            string result = String.Empty;            result = HttpC ...

  8. C#获取外网IP地址;C#获取所在IP城市地址

    public static string GetIP()         {             using (var webClient = new WebClient())           ...

  9. C#获取外网IP、本机MAC地址及Ping的实现

    原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表 ...

随机推荐

  1. Tuxera ntfs软件如何删除干净

    sudo /Library/Filesystems/fusefs_txantfs.fs/Contents/Resources/Support/uninstall-package.sh

  2. phalcon常用语法

    打印SQL //在config/service.php中注册服务 $di->set( 'profiler', function () { return new \Phalcon\Db\Profi ...

  3. Shell 语法之用户输入

    bash shell 提供了一些不同的方法从用户处获取数据,这些方法包括命令行参数.命令行选项和直接读取键盘输入. 命令行参数 bash shell 将在命令行中输入的所有参数赋值给一些特殊变量,称为 ...

  4. 【JAVA】Class.getResource()与ClassLoader.getResource()的区别

    转载自:https://blog.csdn.net/qq_33591903/article/details/91444342 Class.getResource()与ClassLoader.getRe ...

  5. Java通过接口或者抽象类调用方法的时候,怎么知道调用的是哪个实现类里的方法?

    用对象的 getClass() 方法获得它的类,之后就可以随意去判断这是哪个实现类了. 比如代码1-1所示的JDBC连接池的代码,我们想要知道conn调用的close方法是释放连接还是归还连接, 我们 ...

  6. js前台中获取后台传的值

    后台controller String ifOffice = "yes";req.setAttribute("ifOffice", ifOffice); 前台j ...

  7. SQL 查询--日期条件(今日、昨日、本周、本月。。。) (转)

    主要用到sql 函数 DATEDIFF(datepart,startdate,enddate) sql 语句,设 有 数据库表 tableA(日期字段ddate) ——查询 今日 select * f ...

  8. 你真的会用Action的模型绑定吗?

    在QQ群或者一些程序的交流平台,经常会有人问:我怎么传一个数组在Action中接收.我传的数组为什么Action的model中接收不到.或者我在ajax的data中设置了一些数组,为什么后台还是接收不 ...

  9. Hadoop 集群硬件配置

  10. CSS作业问题 内容回顾

    CSS作业问题 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...