Android 获取本地外网IP、内网IP、计算机名等信息
一、获取本地外网IP
- public static String GetNetIp()
- {
- URL infoUrl = null;
- InputStream inStream = null;
- try
- {
- //http://iframe.ip138.com/ic.asp
- //infoUrl = new URL("http://city.ip138.com/city0.asp");
- infoUrl = new URL("http://iframe.ip138.com/ic.asp");
- URLConnection connection = infoUrl.openConnection();
- HttpURLConnection httpConnection = (HttpURLConnection)connection;
- int responseCode = httpConnection.getResponseCode();
- if(responseCode == HttpURLConnection.HTTP_OK)
- {
- inStream = httpConnection.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));
- StringBuilder strber = new StringBuilder();
- String line = null;
- while ((line = reader.readLine()) != null)
- strber.append(line + "\n");
- inStream.close();
- //从反馈的结果中提取出IP地址
- int start = strber.indexOf("[");
- int end = strber.indexOf("]", start + 1);
- line = strber.substring(start + 1, end);
- return line;
- }
- }
- catch(MalformedURLException e) {
- e.printStackTrace();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
二、 获取本地内网IP
- // 获取本地IP函数
- public static String getLocalIPAddress() {
- try {
- for (Enumeration<NetworkInterface> mEnumeration = NetworkInterface
- .getNetworkInterfaces(); mEnumeration.hasMoreElements();) {
- NetworkInterface intf = mEnumeration.nextElement();
- for (Enumeration<InetAddress> enumIPAddr = intf
- .getInetAddresses(); enumIPAddr.hasMoreElements();) {
- InetAddress inetAddress = enumIPAddr.nextElement();
- // 如果不是回环地址
- if (!inetAddress.isLoopbackAddress()) {
- // 直接返回本地IP地址
- return inetAddress.getHostAddress().toString();
- }
- }
- }
- } catch (SocketException ex) {
- System.err.print("error");
- }
- return null;
- }
三、 获取本地外网IP、内网IP、计算机名等信息
- /**
- *功能: 获取外网IP,内网IP,计算机名等信息;
- *
- *作者: jef
- *
- *时间: 20100714
- *
- *版本: v1.0.0
- *
- *
- *程序说明:
- * 通过纯真网络来获取IP,因为ip138网站有时不准。
- *
- * 运行程序时命令行参数请输入http://www.cz88.net/ip/viewip778.aspx
- * 等待程序运行完毕(执行时间视网络情况而定),会在程序目录下生成一个GETIP.sys文件来输出各参数。
- *
- * 运行时如果不输入命令行参数,则默认使用http://www.cz88.net/ip/viewip778.aspx来获取IP。
- *
- * 注意,
- * 不输入命令行参数时获取的信息会输出到命令行,不会输出到文件。
- * 输入命令行参数时获取的信息则会输出到文件,不管获取IP成功与否。
- *
- * 输出信息部分内容的含义如下,
- * sucess
- * hostName is:MyPC
- * hostAddr is:192.168.1.114
- * Foreign IP is:210.72.100.9
- * Location is:江苏省苏州 长城宽带
- * ......
- *
- * 第一行表示全部过程成功与否。成功输出"sucess",否则"fail",
- * 第二行表示计算机名,
- * 第三行表示内网IP,
- * 第四行表示外网IP,
- * 第五行表示外网IP所有的可能地理位置(可信度依赖于查询的网站)。
- * ......
- *
- *
- *使用举例:
- * 拷贝 \cn\mail\sendback\GetIP.class 文件到C:\Documents and Settings下。注意要保留包名的目录。
- * 打开命令提示行窗口,输入:
- *
- * c:
- * cd C:\Documents and Settings
- * java cn.mail.sendback.GetIP http://www.cz88.net/ip/viewip778.aspx
- *
- * 等待C:\Documents and Settings目录下出现GETIP.sys文件则表示执行完毕,
- * 用记事本打开该文件。含义见说明部分。
- *
- */
- package com.soai.test;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.InetAddress;
- import java.net.URL;
- import java.net.UnknownHostException;
- import java.util.Date;
- public class GetIP {
- /**
- * @param args
- */
- public static void main(String[] args){
- // 通过纯真网络来获取IP,因为ip138网站有时不准。
- // 运行程序时命令行输入:http://www.cz88.net/ip/viewip778.aspx
- boolean bHasNoArgs =false;
- if(args.length<=0) bHasNoArgs =true;
- StringBuffer sbFileContent =new StringBuffer();
- boolean bGetSuccess =true;
- try {
- InetAddress host =InetAddress.getLocalHost();
- String hostName =host.getHostName();
- String hostAddr=host.getHostAddress();
- String tCanonicalHostName =host.getCanonicalHostName();
- Date da =new Date();
- String osname =System.getProperty("os.name");
- String osversion =System.getProperty("os.version");
- String username =System.getProperty("user.name");
- String userhome =System.getProperty("user.home");
- String userdir =System.getProperty("user.dir");
- if(bHasNoArgs){
- System.out.println("hostName is:"+hostName);
- System.out.println("hostAddr is:"+hostAddr);
- System.out.println("Current Date is:"+da.toString());
- System.out.println("osname is:"+osname);
- System.out.println("osversion is:"+osversion);
- System.out.println("username is:"+username);
- System.out.println("userhome is:"+userhome);
- System.out.println("userdir is:"+userdir);
- }
- else{
- sbFileContent.append("hostName is:"+hostName+"\n");
- sbFileContent.append("hostAddr is:"+hostAddr+"\n");
- sbFileContent.append("Current Date is:"+da.toString()+"\n");
- sbFileContent.append("osname is:"+osname+"\n");
- sbFileContent.append("osversion is:"+osversion+"\n");
- sbFileContent.append("username is:"+username+"\n");
- sbFileContent.append("userhome is:"+userhome+"\n");
- sbFileContent.append("userdir is:"+userdir+"\n");
- }
- StringBuffer url =new StringBuffer();
- if(bHasNoArgs||args[0].equals(null)||args[0].equals("")){
- url.append("http://www.cz88.net/ip/viewip778.aspx");
- }
- else
- url.append(args[0]);
- StringBuffer strForeignIP =new StringBuffer("strForeignIPUnkown");
- StringBuffer strLocation =new StringBuffer("strLocationUnkown");
- if(GetIP.getWebIp(url.toString(),strForeignIP,strLocation)){
- if(bHasNoArgs){
- System.out.println("Foreign IP is:"+strForeignIP);
- System.out.println("Location is:"+strLocation);
- }
- else{
- sbFileContent.append("Foreign IP is:"+strForeignIP+"\n");
- sbFileContent.append("Location is:"+strLocation+"\n");
- }
- }
- else{
- if(bHasNoArgs){
- System.out.println("Failed to connect:"+url);
- }
- else{
- bGetSuccess =false;
- sbFileContent.append("Failed to connect:"+url+"\n");
- }
- }
- } catch (UnknownHostException e) {
- if(bHasNoArgs){
- e.printStackTrace();
- }
- else{
- bGetSuccess =false;
- sbFileContent.append(e.getStackTrace()+"\n");
- }
- }
- if(bGetSuccess)
- sbFileContent.insert(0,"sucess"+"\n");
- else
- sbFileContent.insert(0,"fail"+"\n");
- if(!bHasNoArgs) write2file(sbFileContent);
- }
- public static boolean getWebIp(String strUrl,
- StringBuffer strForeignIP,StringBuffer strLocation) {
- try {
- URL url = new URL(strUrl);
- BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
- String s = "";
- StringBuffer sb = new StringBuffer("");
- while ((s = br.readLine()) != null) {
- sb.append(s + "\r\n");
- }
- br.close();
- String webContent = "";
- webContent = sb.toString();
- if( webContent.equals(null)|| webContent.equals("") ) return false;
- 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));
- return true;
- } catch (Exception e) {
- //e.printStackTrace();
- return false;
- }
- }
- public static void write2file(StringBuffer content){
- if(content.length()<=0) return;
- try {
- FileOutputStream fos = new FileOutputStream("GETIP.sys");
- OutputStreamWriter osr =new OutputStreamWriter(fos);
- BufferedWriter bw =new BufferedWriter(osr);
- try {
- int index =0;
- while(index>=0){
- int preIndex =index;
- index =content.indexOf("\n", preIndex+2);
- if(index>0){
- String str =new String(content.substring(preIndex, index));
- bw.write(str);
- bw.newLine();
- }
- else{
- String str =new String(content.substring(preIndex, content.length()-1));
- bw.write(str);
- break;
- }
- }
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- //e1.printStackTrace();
- }
- try {
- bw.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- //e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- //e.printStackTrace();
- }
- }
- }
Android 获取本地外网IP、内网IP、计算机名等信息的更多相关文章
- 如何通过IP地址分辨公网、私网、内网、外网
如何通过IP地址分辨公网.私网.内网.外网 内.外网是相对于防火墙而言的,在防火墙内部叫做内网,反之就是外网. 在一定程度上外网等同于公网,内网等同于私网. 地址为如下3个区域就是处于私网 ...
- 外网访问内网的FTP服务器
转自 外网访问内网的FTP服务器 首先感谢作者给出的总结,原文是介绍Serv-U的,我针对FileZilla Server进行了稍微修改,仅看操作可直接跳到分割线后第3部分. 1. 背景简介最近研究如 ...
- 简单物联网:外网访问内网路由器下树莓派Flask服务器
最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...
- 利用ssh反向代理以及autossh实现从外网连接内网服务器
前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...
- 外网访问内网Docker容器
外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...
- 怎样从外网访问内网Rails
外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...
- 怎样从外网访问内网Memcached数据库
外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...
- 怎样从外网访问内网CouchDB数据库
外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...
- 怎样从外网访问内网DB2数据库
外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...
随机推荐
- CSS中 Zoom属性
CSS中 Zoom属性 其实Zoom属性是IE浏览器的专有属性,Firefox等浏览器不支撑.它可以设置或检索对象的缩放比例.除此之外,它还有其他一些小感化,比如触发ie的hasLayout属性,清除 ...
- Docker深入浅出3-镜像管理
当运行容器的时候,使用的镜像如果在本地中不存在,docker就会自动从docker镜像仓库中下载,默认是从dockerhub公共镜像源下载. 1:镜像列表 我们可以使用docker images [r ...
- GetLastError函数
错误代码各个位数的意义:GetLastError函数返回值!SetLastError可是设置这个错误代码. 位 31-30 29 28 27-16 15-0 内容 严重性 Micorsoft/ 客 ...
- 命名空间“Microsoft.Office.Interop”中不存在类型或命名空间名称“Excel”。是否缺少程序集引用 的另一种解决方案
一直以来都是使用tfs进行源代码管理,系统部署也是由我本机生成后发布到服务器上,某一日,进行发布操作时,报了 [命名空间“Microsoft.Office.Interop”中不存在类型或命名空间名称“ ...
- 服务器返回的json数据中含有null的处理方法
个人博客:http://guohuaden.com/2017/03/06/json-dataNull/因为有遇到过类似情况,所以就想到了一些解决方法,并且实践了一下,这里简单的做个记录. 注:有看到不 ...
- Java设计模式应用——过滤器模式
storm引擎计算出一批中间告警结果,会发送一条kafka消息给告警入库服务,告警入库服务接收到kafka消息后读取中间告警文件,经过一系列处理后把最终告警存入mysql中. 实际上,中间告警结果可能 ...
- Unity VR编辑器――如上帝般创建VR内容,Project Soli google用雷达识别手势体积相当于一张 Mini SD 内存卡
Unity VR编辑器――如上帝般创建VR内容在GDC的一个活动中,Unity首席设计师Timoni West展示了最新的Unity VR编辑器的原型系统,让你如上帝般创建VR应用,从一片空白场景开始 ...
- JS实现仿腾讯微博无刷新删除微博效果代码
这里演示JS仿腾讯微博无刷新删除效果,将显示在微博列表里的内容删除,运用AJAX技术,无刷新删除微博的内容,参考性强,希望对初学AJAX的朋友有所帮助. 在线演示地址如下: http://demo.j ...
- C/C++之static函数与普通函数
全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式.这两者在存储方式上并无不同.这两者的区别虽在于非静态全局变量 ...
- Linux中Postfix邮件安装Maildrop(八)
Postfix使用maildrop投递邮件 Maildrop是本地邮件投递代理(MDA), 支持过滤(/etc/maildroprc).投递和磁盘限额(Quota)功能. Maildrop是一个使用C ...