问题
I'm trying to include one of the Maxmind databases in my Java application. The database I am using is the latest version of GeoLite2-City.mmdb
.
When I drop the database file into the project (src/main/resources/GeoLite2-City.mmdb
) and I run the project I am able to use the database via com.maxmind.geoip2.DatabaseReader
and perform lookups of IP addresses.
However once I compile the project and create a jar file containing the database I am unable to read the database in the file, I get the exception:
com.maxmind.db.InvalidDatabaseException: Could not find a MaxMind DB metadata marker in this file (<InputStream>). Is this a valid MaxMind DB file?
Unzipping the .jar file and looking at the mmdb file I can see that the size of the file has changed from 38Mb to 59Mb, which I'm guessing is the reason for the exception being thrown.
Does anyone know of a way of packaging this file in a .jar so that it is usable? I've tried adding the original .gz file into the package, then decompressing that and then loading the database but that doesn't work either.
Thanks
回答1:
You don't mention how you are creating the JAR, but assuming you are using Maven, you need to disable resource filtering for binary files, e.g.:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
...
<nonFilteredFileExtensions>
<nonFilteredFileExtension>mmdb</nonFilteredFileExtension>
</nonFilteredFileExtensions>
...
</configuration>
</plugin>
来源:https://stackoverflow.com/questions/34449733/geolite2-database-gets-corrupt-when-added-to-jar