IP工具类-自己动手做个ip解析器

一、资料准备

导入依赖包:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAicAAAC7CAIAAAAxCambAAAbLElEQVR4nO2dS2scR7uA5z8FvOh9RrcJIbsssnA2vQnSLpuAwUlwFJM4aYXwGR2OOSLZCATZCAa0EpIle3yRZSV4NRAIljSjkWzJ0fILrrPoW1V1VXXP9KQk28/Di2z1tbrVU0+/VT1dDTESz1cXWle/uLWjTu2tz11bf57/Pli5ttxJ/vNF66oSc6uD0XYNADAuDg4OjNOfry7MrQ46iwu3FhdaV79oXY2rsme3jPXe4rN4llLLLT6T5mpkdaMQQnQW023uLM+tDoR4divZnTTrDUc+z40R1n++utC6tv5ciM7iwkovnbqz3MqVU9RMforFW3QqAeCNxmKdZ7eufhFbJ63iYqlINV5Gb31u8VlsqXiCUr9Vs456O64I7O2oKutaRyIxf2cxFbs+N7lBWFkdiPT2oZASAQBcDEbrPF9dmLum5ToLKz1JBnJ111ufW1xfufZFets9WLkmyam3PndVb+nRbsTJdYbD3NQWs7PcUu8O5NsBAIALx2idzup6J21hi6uv56sLSVXWGyR3zKpO5lafpenLs1vyXfWIuQ7WMSGfKVOHzWBlcfnWteVO/DfYWW4lSeizlWtvyXkEgDed0n6dOMWZS1tr8tvorM0m9Uq8in5vPWK/Di1sKrFv4gxUn5ecNZEKJuuCW+7kuc6zW8bmUQAAv1SwjhAiyWzmVgd5A5qhp8DU91NmneLt+9zicrZlch0Fc3NZap3O4nInObODLLnJV9lZbl1bH3nXAABjwW0dIUTybFSsgZ3lPPnIrJM/SPXs1tUvWpqKKuQ6ya+99bm4YW1nOes3wjoKcY+OvYVNFM4s/ToAcLmoYB0hRNpLrT1EEFd619afyz3cO8tKulP+NIH09MHOcmtxXX4YAesouHMdIYRineTJgrfkDALA20G5dYq+iclSnLzxLWOwcq3q93V0r0idFMXvnLy5jPMZNgCANxebdWC8YB0AACGwji+wDgCAEFjHF1gHAEAIrOMLrAMAIATW8QXWAQAQAuv4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JgfbE0dbE0dbE/27zf5ms7fZ7G00Dzfe7200+9sfjausbz96uyMAwNsGo7oBAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP7AOAAD4A+sAAIA/sA4AAPgD6wAAgD+wDgAA+APrAACAP/4fSCNp9sj6aqkAAAAASUVORK5CYII=" alt="" />

下载地址: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 。

IP工具类-自己动手做个ip解析器的更多相关文章

  1. IP工具类

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletReques ...

  2. Java常用工具类---IP工具类、File文件工具类

    package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...

  3. 系统获取 IP 工具类

    系统获取 IP 工具类 import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterf ...

  4. Java 获取IP工具类、Vo类整理记录

    前言 日常开发中,获取ip是常用的功能,本文记录如何在Java中获取本机外网ip.地理位置,访问用户的外网ip.地理位置,以及指定外网ip的地理位置: 代码编写 1.获取访问用户外网ip,我们从访问者 ...

  5. java获取真实ip工具类

    场景 有的时候我们需要获取客户端的真实ip,用来实现ip白名单,和黑名单的配置! ip工具类如下 package com.meeno.framework.utils; import javax.ser ...

  6. 获取请求的ip工具类

    package com.example.util; import javax.servlet.http.HttpServletRequest; /** * get remote msg * 获取访问的 ...

  7. C#获取客户端Ip工具类

    string pcname = Dns.GetHostName(); string ip = Dns.GetHostAddresses(pcname).First().ToString(); usin ...

  8. 【java工具类】java做的一个xml转Excel工具,基于maven工程

    说明:适合数据库导出为xml时转成Excel 本工具将上传至GitHub:https://github.com/xiaostudy/xiaostudyAPI3 doc4j的maven依赖 <!- ...

  9. Lucene第二篇【抽取工具类、索引库优化、分词器、高亮、摘要、排序、多条件搜索】

    对Lucene代码优化 我们再次看回我们上一篇快速入门写过的代码,我来截取一些有代表性的: 以下代码在把数据填充到索引库,和从索引库查询数据的时候,都出现了.是重复代码! Directory dire ...

随机推荐

  1. C#设计模式——桥接模式(Bridge Pattern)

    一.概述在软件开发中,我们有时候会遇上一个对象具有多个变化维度.比如对汽车对象来说,可能存在不同的汽车类型,如公共汽车.轿车等,也可能存在不同的发动机,如汽油发动机.柴油发动机等.对这类对象,可应用桥 ...

  2. .net开发微信公众平台

    一.说明:公众平台信息接口为开发者提供了一种新的消息处理方式,只有申请成为开发者后,你才能使用公众平台的开发功能,在这里你需要填写一个URL和一个Token,这两项信息也需要你拥有自己的服务器(外网服 ...

  3. 【jQuery基础学习】09 jQuery与前端(这章很水)

    这章主要是将如何将jQuery应用到网站中,或者说其实就是一些前端知识,对于我这种后端程序来说其实还是蛮有用的. 关于网站结构 文件结构 前端文件分三个文件夹放 images文件夹用来存放将要用到的图 ...

  4. Javascript定时跳转

    因为做项目,用到跳转回上级页面,这里设置定时3秒跳转到目标页面 <script> setInterval("myInterval()",3000);//1000为1秒钟 ...

  5. eclipse优化与标准化记录

    1.文件使用UTF-8格式: 2.取消js验证: 3.设置java文件模板

  6. java分派

    变量被声明时的类型叫做变量的静态类型(Static Type) 又叫明显类型(Apparent Type).变量所引用的对象的真实类型又叫做变量的实际类型(Actual Type). 根据对象的类型而 ...

  7. CSS3边框温故

    1.简介:border属性在CSS1中就已经定义了,用来设置元素边框风格,设置不同的边框.颜色.粗细 2.基本属性,包括三个类型值:(1)border-width:设置元素边框的粗细,默认3~4px( ...

  8. art-template引擎模板

    art-template简介 artTemplate(后文简称aT)才是模板引擎,而TmodJS(后文简称TJ,曾用名atc)则是依赖于前者的一款模板预编译器.两者都是由腾讯开发.其实aT完全可以独立 ...

  9. Force.com微信开发系列(六)客服接口

    当用户主动发消息给微信公众账号的时候(包括发送信息.点击自定义菜单click事件.订阅事件.扫描二维码事件.支付成功事件.用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前为48小时 ...

  10. Android 开发前的基本的配置及第一个Android 程序

     一.JDK 1. 网上下载JDK 2.   配置环境变量(网上百度相关资料)       JAVA_HOME=D:\Java\jdk1.8.0_91       CLASSPATH=.;%JAVA_ ...