java根據ip獲取城市


需求簡介:

  后台管理系統, 需要記錄在線用戶, 現在只差在線用戶的詳細位置,

 

網上一類教程不少, 有些稍微有些彎路, 因此在這里記錄一下我的解決方案

 

自定義地址工具類

 1 import java.io.*;
 2 import java.net.HttpURLConnection;
 3 import java.net.URL;
 4 
 5 public class AddressUtils {
 6 
 7     /**
 8      *
 9      * @param content
10      * @param encodingString
11      * @return
12      * @throws UnsupportedEncodingException
13      */
14     public String getAddresses(String content, String encodingString)
15             throws UnsupportedEncodingException {
16         // 這里調用接口
17         String urlStr = "http://whois.pconline.com.cn/ip.jsp";
18         // 從http://whois.pconline.com.cn取得IP所在的省市區信息
19         String returnStr = this.getResult(urlStr, content, encodingString);
20         if (returnStr != null) {
21             // 處理返回的省市區信息
22             String[] temp = returnStr.split(" ");
23             if(temp.length<2){
24                 return "0";//無效IP
25             }
26             String region = temp[0];
27             if(StringUtil.isNull(region) || region.trim().equals("")){
28                 region = temp[1];
29             }
30             return region;
31         }
32         return "未知";
33     }
34 
35     /**
36      * @param urlStr
37      * @param content
38      * @param encoding
39      * @return
40      */
41     private String getResult(String urlStr, String content, String encoding) {
42         URL url = null;
43         HttpURLConnection connection = null;
44         try {
45             url = new URL(urlStr);
46             connection = (HttpURLConnection) url.openConnection();// 新建連接實例
47             connection.setConnectTimeout(2000);// 設置連接超時時間,單位毫秒
48             connection.setReadTimeout(2000);// 設置讀取數據超時時間,單位毫秒
49             connection.setDoOutput(true);// 是否打開輸出流 true|false
50             connection.setDoInput(true);// 是否打開輸入流true|false
51             connection.setRequestMethod("POST");// 提交方法POST|GET
52             connection.setUseCaches(false);// 是否緩存true|false
53             connection.connect();// 打開連接端口
54             DataOutputStream out = new DataOutputStream(connection
55                     .getOutputStream());// 打開輸出流往對端服務器寫數據
56             out.writeBytes(content);// 寫數據,也就是提交你的表單 name=xxx&pwd=xxx
57             out.flush();// 刷新
58             out.close();// 關閉輸出流
59             BufferedReader reader = new BufferedReader(new InputStreamReader(
60                     connection.getInputStream(), encoding));// 往對端寫完數據對端服務器返回數據
61             // ,以BufferedReader流來讀取
62             StringBuffer buffer = new StringBuffer();
63             String line = "";
64             while ((line = reader.readLine()) != null) {
65                 buffer.append(line);
66             }
67             reader.close();
68             return buffer.toString();
69         } catch (IOException e) {
70             e.printStackTrace();
71         } finally {
72             if (connection != null) {
73                 connection.disconnect();// 關閉連接
74             }
75         }
76         return null;
77     }
78 
79     /**
80      * 
81      * @param ip
82      * @return
83      */
84     public static String getCity(String ip) {
85         AddressUtils addressUtils = new AddressUtils();
86         String address = "";
87         try {
88             address = addressUtils.getAddresses("ip="+ip, "gbk");
89         } catch (UnsupportedEncodingException e) {
90             // TODO Auto-generated catch block
91             e.printStackTrace();
92         }
93         return address;
94     }
95 }

 

這里獲取地址的接口沒有用淘寶接口, 因為現在淘寶接口需要一個公鑰, 否則會一直提示這個錯誤

{"msg":"the request over max qps for user ,the accessKey=public","code":4}

 

調用下面這個方法即可獲取

AddressUtils.getCity(ip);

 

它的最后返回結果是這樣的

xx省xx市 電信

 

需要省市可以在 getAddresses 方法中做一下處理, 若是局域網, 則會顯示

 局域網IP

需要注意的是, " 局域網IP" 前面會有一個空格, 若按照空格拆分時需注意

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM