【发布时间】:2015-03-03 20:39:14
【问题描述】:
我的最终目标是获取与提供的 IP 地址相关的纬度、经度和国家/地区。我正在使用 java 和 MaxMind GeoIP2 Java API。我的偏好是不使用他们的 webapi,因为我需要快速周转时间。
谁能告诉我为什么在调用与 MaxMind api 调用相关的函数时出现此错误(错误后显示的代码)?
我不确定 MaxMind DB 元数据标记是什么。它抱怨我从 MaxMind 网站上抓取的文件。我会假设它是一个有效的 MaxMind DB 文件。
com.maxmind.db.InvalidDatabaseException: Could not find a MaxMind DB metadata marker in this file (GeoLite2-City-Blocks-IPv6.csv). Is this a valid MaxMind DB file?
at com.maxmind.db.Reader.findMetadataStart(Reader.java:231)
at com.maxmind.db.Reader.<init>(Reader.java:82)
at com.maxmind.db.Reader.<init>(Reader.java:74)
at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:41)
at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:31)
at com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:126)
at com.tutelatechnologies.tapfortap.server.maxmind.MaxMindLookUp.maxMindLookup(MaxMindLookUp.java:20)
at com.tutelatechnologies.tapfortap.server.cidlaclookup.T4TUtilitiesTest.testMaxMindLookUpTest(T4TUtilitiesTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
我按照 MaxMind 网站上的说明和示例代码进行了数据使用部分:http://maxmind.github.io/GeoIP2-java/
我选择使用从这里获得的 GeoLite2 csv dbs:http://dev.maxmind.com/geoip/geoip2/geolite2/
我的代码:
public class MaxMindLookUp {
public static final void maxMindLookup(final String ipaddress) {
File db = new File(
"src/main/java/com/tutelatechnologies/tapfortap/server/maxmind/GeoLite2-City-Blocks-IPv6.csv");
try {
DatabaseReader reader = new DatabaseReader.Builder(db).build();
InetAddress ip = InetAddress.getByName(ipaddress);
CityResponse response = reader.city(ip);
Location loc = response.getLocation();
Double lat = loc.getLatitude();
Double lng = loc.getLongitude();
Integer acc = loc.getAccuracyRadius();
System.out.println("lat=" + lat + "\nlng=" + lng + "\nacc=" + acc);
} catch (GeoIp2Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
注意:为了运行上面的代码,您需要在 Maven 依赖项部分添加一个插件。
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.1.0</version>
</dependency>
我没有遇到太多关于 SO 的确切错误。我已经通过调试器,错误发生在这一行:
DatabaseReader reader = new DatabaseReader.Builder(db).build();
这让我相信我从网站上下载的数据库有问题。
此页面底部的异常标题 (http://maxmind.github.io/GeoIP2-java/) 提供了一个死链接“GeoIP2 Precision Web 服务文档”,因此没有任何帮助。
在他们的 Java api 的 Exceptions 部分中似乎没有与此异常相关的任何内容:http://maxmind.github.io/GeoIP2-java/doc/v2.1.0/
更新
根据此处规范中的 DatabaseReader.Builder().build() 描述:http://maxmind.github.io/GeoIP2-java/doc/v2.1.0/ 我认为问题来自读取数据库。我还不确定如何解决这个问题。
更新
我能够使用二进制 db 文件 (GeoLite2-City.mmdb) 让它工作。如果有人知道为什么 CSV db 文件不适合我,我会留下这个问题。
【问题讨论】:
标签: java geolocation maxmind