本人的思路是讓程序去訪問(wèn)(HttpURLConnection)一個(gè)可以查IP地址的,然后把這個(gè)網(wǎng)頁(yè)下載(FileOutputStream)到本地,通過(guò)(BufferedReader)去讀取那個(gè)臨時(shí)文件,最后通過(guò)操作文件(File),把臨時(shí)文件刪除,并以彈出“對(duì)話框”形式把獲取的“外網(wǎng)IP”顯示出來(lái)。
提供幾個(gè)查IP反回頁(yè)面相對(duì)簡(jiǎn)單的網(wǎng)址,訪問(wèn)不同的網(wǎng)址,讀取關(guān)鍵字符串的時(shí)候可能會(huì)不同,請(qǐng)做出相應(yīng)的修改。
下面是程序的源碼,可以參考一下:
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 * 輸入?yún)?shù)數(shù)組
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, "您的外網(wǎng)IP地址",
32 JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
33 }
34
35 /**/
36 /**
37 * 獲取已下載的文件里的字符串(IP地址)
38 *
39 * @return 獲取的外網(wǎng)IP地址
40 * @throws Exception
41 */
42 public static String GetPublicIP(String urlStr, String tempSaveStr) {
43
44 // 下載操作 - 開(kāi)始 :下載網(wǎng)絡(luò)文件獲取相關(guān)IP地址并保存為臨時(shí)文件IP.shtml
45 int chByte = 0; // 讀入輸入流的數(shù)據(jù)長(zhǎng)度
46 URL url = null; // 網(wǎng)絡(luò)的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 // 下載操作 - 結(jié)束
81
82 // 獲取IP操作 - 開(kāi)始 : 從臨時(shí)文件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操作 - 結(jié)束
93
94 // 刪除操作 - 開(kāi)始 :刪除臨時(shí)文件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 // 刪除操作 - 結(jié)束
103
104 return IP;
105 }
106 }
對(duì)于源碼中第55,56兩行,我是設(shè)置為模擬IE下載的,可以通過(guò)修改,達(dá)到模擬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");