什么是GeoIP ?
所谓GeoIP,就是通过来访者的IP, 定位他的经纬度,国家/地区,省市,甚至街道等位置信息的一个数据库。GeoIP有两个版本,一个免费版,一个收费版本。收费版本的准确率和数据更好一些。
GeoIP如何使用?
GeoIP支持多种语言调用,这里我们以java为例。
执行下面的命令:
wget -N -q http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gzip -d GeoIP.dat.gz
cp GeoIP.dat /usr/local/share/GeoIP/GeoIP.dat
具体执行代码:
package com.test.intl.commons.ip;
/**
* 这个类封装了对国家的数据。 类Country.java的实现描述:TODO 类实现描述
*
*/
public class Country {
private String code;
private String name;
/**
* Creates a new Country.
*
* @param code the country code.
* @param name the country name.
*/
public Country(String code, String name) {
this.code = code;
this.name = name;
}
/**
* Returns the ISO two-letter country code of this country.
*
* @return the country code.
*/
public String getCode() {
return code;
}
/**
* Returns the name of this country.
*
* @return the country name.
*/
public String getName() {
return name;
}
}
package com.test.intl.commons.ip;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import org.apache.log4j.Logger;
import sun.net.util.IPAddressUtil;
import com.maxmind.geoip.DatabaseInfo;
public class CountryLookupService {
/**
* 这个是数据文件的。
*/
private File databaseFile = null;
/**
* buffer最大的数据。
*/
private int maxBuffer = 0;
/**
* 判断是否进行了初始化工作。
*/
private boolean isInit = false;
//
private static Logger log = Logger.getLogger(CountryLookupService.class);
/**
* 每次进行check的时间间隔,默认是1分钟一次。
*/
private int checkfreshInvertal = 60 * 1000;
/**
* 最后一次做check的时间间隔。
*/
private long lastcheckfreshTime = 0;
/**
* Database file.
*/
private SafeBufferRandomAccessFile file = null;
/**
* Information about the database.
*/
// private DatabaseInfo databaseInfo = null;
/**
* The database type. Default is the country edition.
*/
byte databaseType = DatabaseInfo.COUNTRY_EDITION;
int databaseSegments[];
int recordLength;
String licenseKey;
int dnsService = 0;
private final static int COUNTRY_BEGIN = 16776960;
private final static int STATE_BEGIN = 16700000;
private final static int STRUCTURE_INFO_MAX_SIZE = 20;
// private final static int DATABASE_INFO_MAX_SIZE = 100;
private final static int SEGMENT_RECORD_LENGTH = 3;
private final static int STANDARD_RECORD_LENGTH = 3;
private final static int ORG_RECORD_LENGTH = 4;
private final static int MAX_RECORD_LENGTH = 4;
// private final static int MAX_ORG_RECORD_LENGTH = 300;
// private final static int FULL_RECORD_LENGTH = 50;
public static final Country UNKNOWN_COUNTRY = new Country("--", "N/A");
private final static HashMap hashmapcountryCodetoindex = new HashMap(512);
private final static HashMap hashmapcountryNametoindex = new HashMap(512);
private final static String[] countryCode = { "--", "AP", "EU", "AD", "AE", "AF",
"AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB", "BD", "BE", "BF",
"BG", "BH", "BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF",
"CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM",
"DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB",
"GD", "GE", "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM",
"HN", "HR", "HT", "HU", "ID", &