转自: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;

}

最新文章

  1. 说说&amp;和&amp;&amp;的区别
  2. .Net Core 学习 (1) - ASP.NET Core 总览
  3. youphp学习整理
  4. 安装AppManager
  5. javascript 自己主动绑定JS callback 的方法函数
  6. MySQL存储汉字
  7. vb6.0的各种SHELL,CMD内部命令、外部命令、SHELL任意文件
  8. 【一天一道LeetCode】#349. Intersection of Two Arrays
  9. GoLang simple-project-demo-02
  10. 拥抱单页网站! jQuery全屏滚动插件fullPage.js
  11. Online tools
  12. Kubernetes 服务入口管理与 Nginx Ingress Controller
  13. jQuery懒加载插件 – jquery.lazyload.js简单调用
  14. 理解OpenShift(4):用户及权限管理
  15. linux普通用户使用1024以下的端口(80)
  16. Fluent UDF【7】:解释型UDF
  17. ansible register基础使用讲解
  18. 06: linux中find查找命令总结
  19. 铁乐学Python_day04-列表LIST
  20. CentOS 7下安装Python3.6和pip

热门文章

  1. MFC编译Freetype2.3.7
  2. Odoo加载机制指导流程
  3. 关于dictionary和tuple充当函数参数
  4. Java中String类的常见面试题
  5. Spring BatchSqlUpdate.updateByNamedParam例子
  6. HashMap基础知识
  7. python collections 模块 之namedtuple
  8. Luogu P1278 单词游戏(dfs)
  9. loj2544 「JXOI2018」游戏
  10. java并发系列(五)-----如何正确的关闭一个线程