转自: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. C#端一个不错的订单号生成规则

    /// <summary> /// 订单助手 /// </summary> public class OrderHelper { /// <summary> /// ...

  2. [Swoole系列入门教程 4] 定时器与心跳demo

  3. object and namespace

    http://effbot.org/zone/python-objects.htm 几点总结: (1) 类的基本属性 . id, returned by id(obj) . type, returne ...

  4. Vue简单评星效果与单张图片上传

    <form class="" id="pj-frm"> <div class="assess-header"> &l ...

  5. 获取地址栏URL中参数, getQuerySting()方法

    今天同事用的以前的获取url地址参数获取不到.以前的方法失效了.后面发现是正则表达式bug: 第一种获取方法(针对普通情况的一般够用): function getQueryString(name) { ...

  6. 深入浅出 Java Concurrency (18): 并发容器 part 3 ConcurrentMap (3)[转]

    在上一篇中介绍了HashMap的原理,这一节是ConcurrentMap的最后一节,所以会完整的介绍ConcurrentHashMap的实现. ConcurrentHashMap原理 在读写锁章节部分 ...

  7. Maven实战06_坐标和邮件服务模块

    1:何为Maven坐标 为了能够自动化地解析任何一个Java构件,Maven就必须要将其唯一标识,这就是依赖管理的底层基础--坐标. 学过数学的人都知道平面直角坐标系,x,y分别为其横,纵坐标,将会在 ...

  8. Luogu P2051 [AHOI2009]中国象棋(dp)

    P2051 [AHOI2009]中国象棋 题面 题目描述 这次小可可想解决的难题和中国象棋有关,在一个 \(N\) 行 \(M\) 列的棋盘上,让你放若干个炮(可以是 \(0\) 个),使得没有一个炮 ...

  9. 【转载】传统以太网和时间敏感网络TSN的区别

    转载连接:http://www.proav-china.com/News/16800.html ——Biamp亚太区高级工程师   Kane Zhang [专业视听网报道]:[摘要]AVB-Audio ...

  10. c++设计模式:策略模式

    1.主要思想:例如针对不同的算法,创建不同的类. #include <iostream> using namespace std; // The abstract strategy cla ...