IP工具类-自己动手做个ip解析器
IP工具类-自己动手做个ip解析器
一、资料准备
导入依赖包:
下载地址:http://yunpan.cn/cLHfYZQMn65Qq 访问密码 6090
如果不用阿里巴巴的请求。则不用导入阿里巴巴的jar包。只需导入解析纯真数据库需要的jar即可。
JSon包可导可不导。
二、实现代码
方法一、通过查询数据库实现
方法二、通过调用alibaba的ip数据库查询得出。
以下是详细的实现代码:
package com.souvc.common.base; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.net.UnknownHostException; import javax.servlet.http.HttpServletRequest; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.handsome.ip.IPSeeker; /** * 类名: IPUtil </br> * 描述: ip 工具类 </br> * 开发人员:souvc</br> * 创建时间: 2015-11-10 </br> * 发布版本:V1.0 </br> */ public class IPUtil { /** * 方法名:getIpAddr</br> * 详述:获取IP地址</br> * 开发人员:souvc </br> * 创建时间:2015-11-10 </br> * @param request * @return * @throws */ public static String getIpAddr(HttpServletRequest request) { String ipAddress = request.getHeader("x-forwarded-for"); if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); } if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) { // 根据网卡取本机配置的IP InetAddress inet = null; try { inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } ipAddress = inet.getHostAddress(); } } // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length() // = 15 if (ipAddress.indexOf(",") > 0) { ipAddress = ipAddress.substring(0, ipAddress.indexOf(",")); } } return ipAddress; } /** * 方法名:getIpRealAddr</br> * 详述:获取真实名称</br> * 开发人员:souvc</br> * 创建时间:2015-11-10 </br> * @param ip * @return * @throws */ public static String getIpRealAddr(String ip) { //指定纯真数据库的文件名,所在文件夹 String address = ""; try { IPSeeker ipseeker=new IPSeeker("IpToAdd.Dat", "E:\\ipdata"); address = ipseeker.getIPLocation(ip).getCountry(); } catch(Exception e) { } //获得国家地区 return address; } /** * 方法名:getArea</br> * 详述:获取运营商 </br> * 开发人员:souvc </br> * 创建时间:2015-11-10 </br> * @param ip * @return * @throws */ public static String getArea(String ip) { //指定纯真数据库的文件名,所在文件夹 String area = ""; try { IPSeeker ipseeker=new IPSeeker("IpToAdd.Dat", "E:\\ipdata"); area=ipseeker.getArea(ip); } catch(Exception e) { } return area; } /** * 方法名:getIpInfo</br> * 详述:通过IP获取地址(需要联网,调用淘宝的IP库)</br> * 开发人员:souvc </br> * 创建时间:2015-11-10 </br> * @param ip * @return * @throws */ public static String getIpInfo(String ip) { if (ip.equals("本地")) { ip = "127.0.0.1"; } String info = ""; try { URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=" + ip); HttpURLConnection htpcon = (HttpURLConnection) url.openConnection(); htpcon.setRequestMethod("GET"); htpcon.setDoOutput(true); htpcon.setDoInput(true); htpcon.setUseCaches(false); InputStream in = htpcon.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); StringBuffer temp = new StringBuffer(); String line = bufferedReader.readLine(); while (line != null) { temp.append(line).append("\r\n"); line = bufferedReader.readLine(); } bufferedReader.close(); JSONObject obj = (JSONObject) JSON.parse(temp.toString()); if (obj.getIntValue("code") == 0) { JSONObject data = obj.getJSONObject("data"); info += data.getString("country") + " "; info += data.getString("region") + " "; info += data.getString("city") + " "; info += data.getString("isp"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return info; } /** * 方法名:main</br> * 详述:测试方法</br> * 开发人员:souvc </br> * 创建时间:2015-11-10 </br> * @param args * @throws */ public static void main(String[] args) { String country =getIpRealAddr("14.23.59.170"); String area =getArea("14.23.59.170"); String getIpInfo =getIpInfo("14.23.59.170"); System.out.println(country); System.out.println(area); System.out.println(getIpInfo); } }
效果如下:
美国
ATT用户
中国 广东省 广州市 电信
方法三:通过百度api查询得出。
package com.souvc.weixin.util; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import net.sf.json.JSONObject; public class TestIP { public static void main(String[] args) { String httpUrl = "http://apis.baidu.com/apistore/iplookupservice/iplookup"; String httpArg = "ip=117.89.35.58"; String jsonResult = request(httpUrl, httpArg); //System.out.println(jsonResult); JSONObject jasonObject = JSONObject.fromObject(jsonResult); System.out.println(jasonObject); System.out.println(jasonObject.get("errNum")); System.out.println(jasonObject.get("errMsg")); System.out.println(jasonObject.get("retData")); Map map=(Map) jasonObject.get("retData"); System.out.println(map.get("ip")); System.out.println(map.get("country")); System.out.println(map.get("province")); System.out.println(map.get("city")); System.out.println(map.get("district")); System.out.println(map.get("carrier")); } /** * @param urlAll * :请求接口 * @param httpArg * :参数 * @return 返回结果 */ public static String request(String httpUrl, String httpArg) { BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); httpUrl = httpUrl + "?" + httpArg; try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); // 填入apikey到HTTP header connection.setRequestProperty("apikey", "xxx"); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = null; while ((strRead = reader.readLine()) != null) { sbf.append(strRead); sbf.append("\r\n"); } reader.close(); result = sbf.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } }
注意,替换成自己的百度api的apikey 。