数据加载中……
JAVA:获取本机外网IP地址的小工具
      本人的思路是让程序去访问(HttpURLConnection)一个可以查IP地址的,然后把这个网页下载(FileOutputStream)到本地,通过(BufferedReader)去读取那个临时文件,最后通过操作文件(File),把临时文件删除,并以弹出“对话框”形式把获取的“外网IP”显示出来。

      提供几个查IP反回页面相对简单的网址,访问不同的网址,读取关键字符串的时候可能会不同,请做出相应的修改。


      下面是程序的源码,可以参考一下:

  1 import java.io.File;
  2 import java.io.FileReader;
  3 import java.io.BufferedReader;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.net.HttpURLConnection;
  8 import java.net.MalformedURLException;
  9 import java.net.URL;
 10 import javax.swing.JOptionPane;
 11 
 12 /**
 13  * GetPublicIP 类
 14  * 
 15  * @author YeeYang
 16  * 
 17  * @version 1.0
 18  * 
 19  */
 20 public class GetPublicIP {
 21 
 22     /**/
 23     /**
 24      * 主程序入口
 25      * 
 26      * @param args
 27      *            输入参数数组
 28      */
 29     public static void main(String[] args) {
 30         String pIP = GetPublicIP("http://www.bliao.com/ip.phtml""IP_Temp.tmp");
 31         JOptionPane.showConfirmDialog(null, pIP, "您的外网IP地址",
 32                 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
 33     }
 34 
 35     /**/
 36     /**
 37      * 获取已下载的文件里的字符串(IP地址)
 38      * 
 39      * @return 获取的外网IP地址
 40      * @throws Exception
 41      */
 42     public static String GetPublicIP(String urlStr, String tempSaveStr) {
 43 
 44         // 下载操作 - 开始 :下载网络文件获取相关IP地址并保存为临时文件IP.shtml
 45         int chByte = 0// 读入输入流的数据长度
 46         URL url = null// 网络的url地址
 47         HttpURLConnection httpConn = null// http连接
 48         InputStream in = null// 输入流
 49         FileOutputStream out = null// 文件输出流
 50         try {
 51             url = new URL(urlStr);
 52             httpConn = (HttpURLConnection) url.openConnection();
 53             HttpURLConnection.setFollowRedirects(true);
 54             httpConn.setRequestMethod("GET");
 55             httpConn.setRequestProperty("User-Agent",
 56                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
 57 
 58             in = httpConn.getInputStream();
 59             out = new FileOutputStream(new File("tempSaveStr"));
 60 
 61             chByte = in.read();
 62             while (chByte != -1) {
 63                 out.write(chByte);
 64                 // System.out.println(chByte);
 65                 chByte = in.read();
 66             }
 67         } catch (MalformedURLException e) {
 68             e.printStackTrace();
 69         } catch (IOException e) {
 70             e.printStackTrace();
 71         } finally {
 72             try {
 73                 out.close();
 74                 in.close();
 75                 httpConn.disconnect();
 76             } catch (Exception ex) {
 77                 ex.printStackTrace();
 78             }
 79         }
 80         // 下载操作 - 结束
 81 
 82         // 获取IP操作 - 开始 : 从临时文件IP.shtml中读取IP地址
 83         String IP = null;
 84         try {
 85             BufferedReader br = new BufferedReader(
 86                     new FileReader("tempSaveStr"));
 87             IP = br.readLine();
 88             br.close();
 89         } catch (Exception e) {
 90             e.printStackTrace();
 91         }
 92         // 获取IP操作 - 结束
 93 
 94         // 删除操作 - 开始 :删除临时文件IP.shtml
 95         try {
 96             java.io.File myDelFile = new java.io.File("tempSaveStr");
 97             myDelFile.delete();
 98         } catch (Exception e) {
 99             System.out.println("Wrong Del");
100             e.printStackTrace();
101         }
102         // 删除操作 - 结束
103 
104         return IP;
105     }
106 }

      对于源码中第55,56两行,我是设置为模拟IE下载的,可以通过修改,达到模拟FireFox的目的,代码如下:

1 httpConn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14");

posted on 2009-05-19 23:35 YeeYang 阅读(5693) 评论(3)  编辑  收藏 所属分类: 程序设计

评论

# re: JAVA:获取本机外网IP地址的小工具 2010-02-01 11:29 龙屠日

谢谢,我参考了贵文,改写如下,请指教。

http://www.blogjava.net/longturi/archive/2010/02/01/311480.html

  回复  更多评论    

# re: JAVA:获取本机外网IP地址的小工具 2011-04-04 10:12 wEixiNu

学习了。
  回复  更多评论    

# re: JAVA:获取本机外网IP地址的小工具 2012-01-01 16:00 沫夏

非常好的思路!
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: