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" 前面会有一个空格, 若按照空格拆分时需注意

原文地址:https://www.cnblogs.com/wang95529/p/14235372.html