转自: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. Windows API GetShortPathName GetLongPathName

    函数原型: The GetShortPathName function retrieves the short path form of a specified input path. DWORD G ...

  2. 2.Spring【DI】XML方式

    依赖: 在A类中引用了B类,说明A依赖于B. 注入: 使用Spring框架给A类中的B对象的属性赋值. 直接上代码: 1.只使用IOC public class Person { private St ...

  3. 移动端video视频播放问题

    1.视频播放后自动全屏 video添加playsinline  webkit-playsinline属性 2.安卓暂停或播放完毕不能滑动 通过js判断机型,安卓去掉controls属性,ios保留co ...

  4. XML 映射文件

    MyBatis 的真正强大在于它的映射语句,这是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉了将近 95% ...

  5. 左神算法书籍《程序员代码面试指南》——2_02在单链表和双链表中删除倒数第k个字节

    [题目]分别实现两个函数,一个可以删除单链表中倒数第K个节点,另一个可以删除双链表中倒数第K个节点.[要求]如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1).[题解]从头遍历链表, ...

  6. java的堆栈通俗理解

    java内存模型有堆内存和栈内存, 初学者可能看官方解释很模糊 堆:new 出来的对象或者数组都存放在堆中: List <String> list =new ArrayList<St ...

  7. Redis学习笔记03-持久化

    redis是一个内存型数据库,这就意味着,当主机重启或者宕机时,内存中的数据会被清空,redis可能会丢失数据.为了保存数据,实现数据持久化就必须要有一种机制,可以将redis数据库的数据保留在硬盘上 ...

  8. 通过url方式传递中文参数时出现的中文乱码问题的处理

    1.通过url将参数[会有中文的情况]传递到另外一个页面: window.location.href = "/XXXX/XXXX?searchcontent=" + Content ...

  9. matlab中disp函数的简单用法

    输出数组类型的数据,也可以把string类型的数据看做数组输出 输出数字 >> num = ; >> disp(num) 输出字符串 >> disp('this i ...

  10. Idea SSH框架整合基础代码

    第一步 pom.xml注入jar包依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...