一、获取本地外网IP

  1. public static String GetNetIp()
  2. {
  3. URL infoUrl = null;
  4. InputStream inStream = null;
  5. try
  6. {
  7. //http://iframe.ip138.com/ic.asp
  8. //infoUrl = new URL("http://city.ip138.com/city0.asp");
  9. infoUrl = new URL("http://iframe.ip138.com/ic.asp");
  10. URLConnection connection = infoUrl.openConnection();
  11. HttpURLConnection httpConnection = (HttpURLConnection)connection;
  12. int responseCode = httpConnection.getResponseCode();
  13. if(responseCode == HttpURLConnection.HTTP_OK)
  14. {
  15. inStream = httpConnection.getInputStream();
  16. BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));
  17. StringBuilder strber = new StringBuilder();
  18. String line = null;
  19. while ((line = reader.readLine()) != null)
  20. strber.append(line + "\n");
  21. inStream.close();
  22. //从反馈的结果中提取出IP地址
  23. int start = strber.indexOf("[");
  24. int end = strber.indexOf("]", start + 1);
  25. line = strber.substring(start + 1, end);
  26. return line;
  27. }
  28. }
  29. catch(MalformedURLException e) {
  30. e.printStackTrace();
  31. }
  32. catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. return null;
  36. }

二、 获取本地内网IP

  1. // 获取本地IP函数
  2. public static String getLocalIPAddress() {
  3. try {
  4. for (Enumeration<NetworkInterface> mEnumeration = NetworkInterface
  5. .getNetworkInterfaces(); mEnumeration.hasMoreElements();) {
  6. NetworkInterface intf = mEnumeration.nextElement();
  7. for (Enumeration<InetAddress> enumIPAddr = intf
  8. .getInetAddresses(); enumIPAddr.hasMoreElements();) {
  9. InetAddress inetAddress = enumIPAddr.nextElement();
  10. // 如果不是回环地址
  11. if (!inetAddress.isLoopbackAddress()) {
  12. // 直接返回本地IP地址
  13. return inetAddress.getHostAddress().toString();
  14. }
  15. }
  16. }
  17. } catch (SocketException ex) {
  18. System.err.print("error");
  19. }
  20. return null;
  21. }

三、 获取本地外网IP、内网IP、计算机名等信息

    1. /**
    2. *功能: 获取外网IP,内网IP,计算机名等信息;
    3. *
    4. *作者: jef
    5. *
    6. *时间: 20100714
    7. *
    8. *版本: v1.0.0
    9. *
    10. *
    11. *程序说明:
    12. *          通过纯真网络来获取IP,因为ip138网站有时不准。
    13. *
    14. *          运行程序时命令行参数请输入http://www.cz88.net/ip/viewip778.aspx
    15. *          等待程序运行完毕(执行时间视网络情况而定),会在程序目录下生成一个GETIP.sys文件来输出各参数。
    16. *
    17. *          运行时如果不输入命令行参数,则默认使用http://www.cz88.net/ip/viewip778.aspx来获取IP。
    18. *
    19. *          注意,
    20. *          不输入命令行参数时获取的信息会输出到命令行,不会输出到文件。
    21. *          输入命令行参数时获取的信息则会输出到文件,不管获取IP成功与否。
    22. *
    23. *          输出信息部分内容的含义如下,
    24. *          sucess
    25. *          hostName is:MyPC
    26. *          hostAddr is:192.168.1.114
    27. *                 Foreign IP is:210.72.100.9
    28. *          Location is:江苏省苏州 长城宽带
    29. *          ......
    30. *
    31. *          第一行表示全部过程成功与否。成功输出"sucess",否则"fail",
    32. *          第二行表示计算机名,
    33. *          第三行表示内网IP,
    34. *          第四行表示外网IP,
    35. *          第五行表示外网IP所有的可能地理位置(可信度依赖于查询的网站)。
    36. *          ......
    37. *
    38. *
    39. *使用举例:
    40. *           拷贝 \cn\mail\sendback\GetIP.class 文件到C:\Documents and Settings下。注意要保留包名的目录。
    41. *           打开命令提示行窗口,输入:
    42. *
    43. *           c:
    44. *           cd C:\Documents and Settings
    45. *           java cn.mail.sendback.GetIP http://www.cz88.net/ip/viewip778.aspx
    46. *
    47. *           等待C:\Documents and Settings目录下出现GETIP.sys文件则表示执行完毕,
    48. *           用记事本打开该文件。含义见说明部分。
    49. *
    50. */
    51. package com.soai.test;
    52. import java.io.BufferedReader;
    53. import java.io.BufferedWriter;
    54. import java.io.FileNotFoundException;
    55. import java.io.FileOutputStream;
    56. import java.io.IOException;
    57. import java.io.InputStreamReader;
    58. import java.io.OutputStream;
    59. import java.io.OutputStreamWriter;
    60. import java.net.InetAddress;
    61. import java.net.URL;
    62. import java.net.UnknownHostException;
    63. import java.util.Date;
    64. public class GetIP {
    65. /**
    66. * @param args
    67. */
    68. public static void main(String[] args){
    69. // 通过纯真网络来获取IP,因为ip138网站有时不准。
    70. // 运行程序时命令行输入:http://www.cz88.net/ip/viewip778.aspx
    71. boolean bHasNoArgs =false;
    72. if(args.length<=0) bHasNoArgs =true;
    73. StringBuffer sbFileContent =new StringBuffer();
    74. boolean bGetSuccess =true;
    75. try {
    76. InetAddress host =InetAddress.getLocalHost();
    77. String hostName =host.getHostName();
    78. String hostAddr=host.getHostAddress();
    79. String tCanonicalHostName =host.getCanonicalHostName();
    80. Date da =new Date();
    81. String osname =System.getProperty("os.name");
    82. String osversion =System.getProperty("os.version");
    83. String username =System.getProperty("user.name");
    84. String userhome =System.getProperty("user.home");
    85. String userdir =System.getProperty("user.dir");
    86. if(bHasNoArgs){
    87. System.out.println("hostName is:"+hostName);
    88. System.out.println("hostAddr is:"+hostAddr);
    89. System.out.println("Current Date is:"+da.toString());
    90. System.out.println("osname is:"+osname);
    91. System.out.println("osversion is:"+osversion);
    92. System.out.println("username is:"+username);
    93. System.out.println("userhome is:"+userhome);
    94. System.out.println("userdir is:"+userdir);
    95. }
    96. else{
    97. sbFileContent.append("hostName is:"+hostName+"\n");
    98. sbFileContent.append("hostAddr is:"+hostAddr+"\n");
    99. sbFileContent.append("Current Date is:"+da.toString()+"\n");
    100. sbFileContent.append("osname is:"+osname+"\n");
    101. sbFileContent.append("osversion is:"+osversion+"\n");
    102. sbFileContent.append("username is:"+username+"\n");
    103. sbFileContent.append("userhome is:"+userhome+"\n");
    104. sbFileContent.append("userdir is:"+userdir+"\n");
    105. }
    106. StringBuffer url =new StringBuffer();
    107. if(bHasNoArgs||args[0].equals(null)||args[0].equals("")){
    108. url.append("http://www.cz88.net/ip/viewip778.aspx");
    109. }
    110. else
    111. url.append(args[0]);
    112. StringBuffer strForeignIP =new StringBuffer("strForeignIPUnkown");
    113. StringBuffer strLocation =new StringBuffer("strLocationUnkown");
    114. if(GetIP.getWebIp(url.toString(),strForeignIP,strLocation)){
    115. if(bHasNoArgs){
    116. System.out.println("Foreign IP is:"+strForeignIP);
    117. System.out.println("Location is:"+strLocation);
    118. }
    119. else{
    120. sbFileContent.append("Foreign IP is:"+strForeignIP+"\n");
    121. sbFileContent.append("Location is:"+strLocation+"\n");
    122. }
    123. }
    124. else{
    125. if(bHasNoArgs){
    126. System.out.println("Failed to connect:"+url);
    127. }
    128. else{
    129. bGetSuccess =false;
    130. sbFileContent.append("Failed to connect:"+url+"\n");
    131. }
    132. }
    133. } catch (UnknownHostException e) {
    134. if(bHasNoArgs){
    135. e.printStackTrace();
    136. }
    137. else{
    138. bGetSuccess =false;
    139. sbFileContent.append(e.getStackTrace()+"\n");
    140. }
    141. }
    142. if(bGetSuccess)
    143. sbFileContent.insert(0,"sucess"+"\n");
    144. else
    145. sbFileContent.insert(0,"fail"+"\n");
    146. if(!bHasNoArgs) write2file(sbFileContent);
    147. }
    148. public static boolean getWebIp(String strUrl,
    149. StringBuffer strForeignIP,StringBuffer strLocation) {
    150. try {
    151. URL url = new URL(strUrl);
    152. BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    153. String s = "";
    154. StringBuffer sb = new StringBuffer("");
    155. while ((s = br.readLine()) != null) {
    156. sb.append(s + "\r\n");
    157. }
    158. br.close();
    159. String webContent = "";
    160. webContent = sb.toString();
    161. if( webContent.equals(null)|| webContent.equals("") ) return false;
    162. String flagofForeignIPString ="IPMessage";
    163. int startIP = webContent.indexOf(flagofForeignIPString)+flagofForeignIPString.length()+2;
    164. int endIP = webContent.indexOf("</span>",startIP);
    165. strForeignIP.delete(0, webContent.length());
    166. strForeignIP.append(webContent.substring(startIP,endIP));
    167. String flagofLocationString ="AddrMessage";
    168. int startLoc = webContent.indexOf(flagofLocationString)+flagofLocationString.length()+2;
    169. int endLoc = webContent.indexOf("</span>",startLoc);
    170. strLocation.delete(0, webContent.length());
    171. strLocation.append(webContent.substring(startLoc,endLoc));
    172. return true;
    173. } catch (Exception e) {
    174. //e.printStackTrace();
    175. return false;
    176. }
    177. }
    178. public static void  write2file(StringBuffer content){
    179. if(content.length()<=0) return;
    180. try {
    181. FileOutputStream fos = new FileOutputStream("GETIP.sys");
    182. OutputStreamWriter osr =new OutputStreamWriter(fos);
    183. BufferedWriter bw =new BufferedWriter(osr);
    184. try {
    185. int index =0;
    186. while(index>=0){
    187. int preIndex =index;
    188. index =content.indexOf("\n", preIndex+2);
    189. if(index>0){
    190. String str =new String(content.substring(preIndex, index));
    191. bw.write(str);
    192. bw.newLine();
    193. }
    194. else{
    195. String str =new String(content.substring(preIndex, content.length()-1));
    196. bw.write(str);
    197. break;
    198. }
    199. }
    200. } catch (IOException e1) {
    201. // TODO Auto-generated catch block
    202. //e1.printStackTrace();
    203. }
    204. try {
    205. bw.close();
    206. } catch (IOException e) {
    207. // TODO Auto-generated catch block
    208. //e.printStackTrace();
    209. }
    210. } catch (FileNotFoundException e) {
    211. // TODO Auto-generated catch block
    212. //e.printStackTrace();
    213. }
    214. }
    215. }

Android 获取本地外网IP、内网IP、计算机名等信息的更多相关文章

  1. 如何通过IP地址分辨公网、私网、内网、外网

    如何通过IP地址分辨公网.私网.内网.外网   内.外网是相对于防火墙而言的,在防火墙内部叫做内网,反之就是外网.   在一定程度上外网等同于公网,内网等同于私网.   地址为如下3个区域就是处于私网 ...

  2. 外网访问内网的FTP服务器

    转自 外网访问内网的FTP服务器 首先感谢作者给出的总结,原文是介绍Serv-U的,我针对FileZilla Server进行了稍微修改,仅看操作可直接跳到分割线后第3部分. 1. 背景简介最近研究如 ...

  3. 简单物联网:外网访问内网路由器下树莓派Flask服务器

    最近做一个小东西,大概过程就是想在教室,宿舍控制实验室的一些设备. 已经在树莓上搭了一个轻量的flask服务器,在实验室的路由器下,任何设备都是可以访问的:但是有一些限制条件,比如我想在宿舍控制我种花 ...

  4. 利用ssh反向代理以及autossh实现从外网连接内网服务器

    前言 最近遇到这样一个问题,我在实验室架设了一台服务器,给师弟或者小伙伴练习Linux用,然后平时在实验室这边直接连接是没有问题的,都是内网嘛.但是回到宿舍问题出来了,使用校园网的童鞋还是能连接上,使 ...

  5. 外网访问内网Docker容器

    外网访问内网Docker容器 本地安装了Docker容器,只能在局域网内访问,怎样从外网也能访问本地Docker容器? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Docker容器 ...

  6. 怎样从外网访问内网Rails

    外网访问内网Rails 本地安装了Rails,只能在局域网内访问,怎样从外网也能访问本地Rails? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Rails 默认安装的Rails端口 ...

  7. 怎样从外网访问内网Memcached数据库

    外网访问内网Memcached数据库 本地安装了Memcached数据库,只能在局域网内访问,怎样从外网也能访问本地Memcached数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装 ...

  8. 怎样从外网访问内网CouchDB数据库

    外网访问内网CouchDB数据库 本地安装了CouchDB数据库,只能在局域网内访问,怎样从外网也能访问本地CouchDB数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Cou ...

  9. 怎样从外网访问内网DB2数据库

    外网访问内网DB2数据库 本地安装了DB2数据库,只能在局域网内访问,怎样从外网也能访问本地DB2数据库? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动DB2数据库 默认安装的DB2 ...

随机推荐

  1. iOS 自定义日志输出

    在做iOS开发过程中,我们经常需要输出日志来查看某些数据是否打印出来,或者查看查个类是否被调用了. 系统默认的是NSLog(@"xxxx %d",1) ,但如果该APP要发布到商店 ...

  2. 单利模式及python实现方式

    单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...

  3. wcf 服务器无法处理请求由于内部错误

    The server was unable to process the request due to an internal error.  For more information about t ...

  4. SQLServer 重启服务后,自增1的标识列一次增长了1000(转自博问)

    sql2012:我重启了下sql服务,然后自增列Id居然一下子跳了100,怎么回事啊?(之前的数据Id为1,我重启服务后,第二条数据Id就变成1001了),我自增是1,求大神帮忙啊 SQLServer ...

  5. VS2010/MFC编程入门之五十三(Ribbon界面开发:为Ribbon Bar添加控件)

    前面一节中鸡啄米为大家简单介绍了如何创建Ribbon样式的应用程序框架,本节教程就来初步讲讲怎样为Ribbon Bar添加Ribbon控件. VS2010为Ribbon界面开发提供了Ribbon De ...

  6. ArcGIS 10——地理数据库管理GIS数据

    写本文的最初意向是当前正在进行的项目中有实现ESRI版本化数据管理的功能模块,碰到一些棘手的问题,几经周折还是决定系统学习ArcGIS10的帮助文档.(文章摘抄的比较多) 地理数据库是用于保存数据集集 ...

  7. Linux命令:查看文件内容cat|tac|more|less|head|tail|nl|od

    查看文件内容的命令;cat, tac, more, less, head, tail, nl, 1)cat 由第一行开始显示文档内容,一直显示到最后 2)tac 从最后一行开始显示,一直显示到第一行内 ...

  8. Python tricks(5) -- string和integer的comparison操作

    我们都知道, python是一个强类型的语言, 也是一个动态类型的语言. 但是在python2.X系列中, 这个强类型是需要打折扣的, 是非常接近强类型. 我们来看下面的代码片段 In [1]: 'a ...

  9. firefox历史版本下载链接

    http://ftp.mozilla.org/pub/firefox/releases firefox版本42以上的用不了firebug,需要装版本42以下的,否则用不了

  10. Js基础知识2-对象、对象属性全解

    Object对象 Object对象包含如下属性和方法,也就意味着一切对象(函数也是对象)都包含如下方法. 每种方法和属性在不同的对象中有不同的作用,并不是每种对象都有使用每个方法的必要. 下面是Obj ...