首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java geoip2 java.io.FileNotFoundException:

Java geoip2 java.io.FileNotFoundException:
EN

Stack Overflow用户
提问于 2020-04-30 03:12:39
回答 1查看 104关注 0票数 0

我使用geoip2根据ip确定国家/地区。在代码的开发和测试过程中,我没有遇到任何问题,但是当我运行已编译的归档文件时,我遇到了java.io.FileNotFoundException异常。我知道这是因为文件的路径是绝对的,并且在归档文件中它会发生变化。问:我需要如何更改我的代码,才能从归档中访问该文件?

代码语言:javascript
复制
   public static String getCountryByIp(String ip) throws Exception {
    File database = new File(URLDecoder.decode(GeoUtils.class.getResource("/GeoLite2-Country.mmdb").getFile(),"UTF-8"));


    DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
    InetAddress ipAddress = InetAddress.getByName(ip);
    CountryResponse response = dbReader.country(ipAddress);

    return response.getCountry().getName();
}

test.war/ test.war/WEB-INF/classes

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-30 03:19:47

你可以试试这个

代码语言:javascript
复制
InputStream is = this.getClass().getClassLoader().getResourceAsStream("GeoLite2-Country.mmdb");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61509829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档