转自: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. Python PIL 怎么知道写入图片格式的kb大小

    把图片数据写入一个IO,读这个IO的长度大小: #-*-coding:utf-8-*- from PIL import Image import io img = Image.open("1 ...

  2. 2019-8-30-C#-如何在项目引用x86-x64的非托管代码

    title author date CreateTime categories C# 如何在项目引用x86 x64的非托管代码 lindexi 2019-08-30 08:53:52 +0800 20 ...

  3. 如何使用Junit进行单元测试

    测试方法的要求: 必须是public 无返回值 无参数 @Testpublic void f1(){ .....} 在@Test上按下 Ctrl+1(快速锁定错误) 引入Junit包 在方法名上右键 ...

  4. 图解nginx配置负载均衡

    1. 在Linux上准备两份tomcat 2. 修改两份tomcat的端口号 修改的端口如图所示: 3. 启动两个tomcat服务器 4. 修改两个服务器上的主页方便测试区分 5. 在nginx配置文 ...

  5. 关于MySQL IN LIKE OR使用索引的问题

    以前在网上看了一些资料,有些人说话不严谨,导致一直被误导,最近在实际开发中发现一些结论有问题,因此特地整理了一下,防止下次继续犯错. 以下前提是有对这个字段建立索引(简直废话,没建的肯定不会使用索引啊 ...

  6. 抓取摩拜单车API数据,并做可视化分析

    抓取摩拜单车API数据,并做可视化分析 纵聊天下 百家号|04-19 15:16 关注 警告:此篇文章仅作为学习研究参考用途,请不要用于非法目的. 摩拜是最早进入成都的共享单车,每天我从地铁站下来的时 ...

  7. 使用Hilo.JS快速开发Flappy Bird

    http://hiloteam.github.io/tutorial/flappybird.html#_9 Flappy Bird是一款前不久风靡世界的休闲小游戏.虽然它难度超高,但是游戏本身却非常简 ...

  8. PAT甲级——A1044 Shopping in Mars

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  9. hibernate4多对多Use of @OneToMany or @ManyToMany targeting an unmapped class:

    出错之后先查了一下,大家有 @Entity 导错包的,不过我这里没错 import javax.persistence.Entity; 就是这个 还有的是没有注解@Table的,我这里也是没问题的 我 ...

  10. web api中允许跨域访问

    ①添加owin的引用 ②添加owin.Cors的引用 ③在WebApiConfig中添加 config.EnableCors(new EnableCorsAttribute("*" ...