Java源码示例:com.maxmind.geoip.Country
示例1
public Country getCountryData(String ip) {
if( ! hasText(ip)) return null;
try {
Country out = null;
if( ! isIp6_SimpleCheck(ip)) out = ip4Mapper != null? ip4Mapper.getCountry(ip) : null;
else out = ip6Mapper != null? ip6Mapper.getCountryV6(ip) : null;
if(out != null && "--".equals(out.getCode()))
out = null;
return out;
}catch(Exception e){
log.error("can't getCountry for ip "+ip+": "+e);
return null;
}
}
示例2
public String ip2CountryCode(String ip) {
Country country = lookupService.getCountry(ip);
if (country != null) {
return country.getCode();
} else {
return null;
}
}
示例3
/**
* Returns country code for specified ip-addrees, or "-" if lookup fails.
* By using "-" as "unknown country code", these can be filtred out by
* CountryCodeFilter.
*/
public String getCountry(long ipAddress, boolean includeCountryName) {
StringBuilder result = new StringBuilder(64);
Country country = null;
try {
if (geoIpCityManager != null) {
country = geoIpCityManager.getCountry(ipAddress);
} else {
country = geoIpLookup.getCountry(ipAddress);
}
} catch (RuntimeException e) {
// in some rare cases com.maxmind.geoip.LookupService.getCountry throws ArrayIndexOutOfBoundsException
log.error("Cannot lookup country code from ip-adress: " + ipAddress, e);
}
if (country != null) {
log.debug("Country for ip-address " + ipAddress + ": " + country.getCode());
String countryCode = country.getCode();
if (StringUtil.isNullOrEmpty(countryCode) || countryCode.equals("--")) {
countryCode = COUNTRY_CODE_UNKNOWN;
}
result.append(countryCode);
if ((includeCountryName) && !countryCode.equals(COUNTRY_CODE_UNKNOWN)) {
Locale locale = Locale.US;
String name = locale.getDisplayCountry();
result.append(" (").append(name).append(")");
}
} else {
result.append(COUNTRY_CODE_UNKNOWN);
}
return result.toString();
}
示例4
public String getCountry(String ip){
Country out = getCountryData(ip);
return out == null? null : out.getName();
}
示例5
public String getCountryCode(String ip){
Country out = getCountryData(ip);
return out == null? null : out.getCode();
}