1

我正在尝试在 Google App Engine 上使用 MaxMind GeoLite Country 数据库。但是,我很难让 Java API 工作,因为它依赖于 InetAddress 类,该类在 App Engine 上不可用。

但是,我不确定是否有一个简单的解决方法,因为它似乎只使用 InetAddress 类来确定给定主机名的 IP。就我而言,主机名始终是 IP。

我需要一种将表示为字符串的 IP 地址转换为网络字节顺序的字节数组(InetAddress 类的 addr.getAddress() 方法提供的)的方法。

这是当前 API 使用的代码,我需要找到一种方法来删除对 InetAddress 的所有引用,同时确保它仍然有效!

谢谢你的时间。

/**
 * Returns the country the IP address is in.
 *
 * @param ipAddress String version of an IP address, i.e. "127.0.0.1"
 * @return the country the IP address is from.
 */
public Country getCountry(String ipAddress) {
    InetAddress addr;
    try {
        addr = InetAddress.getByName(ipAddress);
    } catch (UnknownHostException e) {
        return UNKNOWN_COUNTRY;
    }
    return getCountry(bytesToLong(addr.getAddress()));
}
4

2 回答 2

1
// note: this is ipv4 specific, and i haven't tried to compile it.
long ipAsLong = 0;
for (String byteString : ipAddress.split("\\."))
{
    ipAsLong = (ipAsLong << 8) | Integer.parseInt(byteString);
}
于 2010-05-02T10:42:39.987 回答
1

GeoIPCountryWhois 数据库中的所有 12000 多个条目均采用以下形式:

"4.17.135.32","4.17.135.63","68257568","68257599","CA","Canada"

将您拥有的虚线四边形地址分解为子字符串并以这种方式检查它们。

于 2010-05-02T11:08:11.727 回答