gpt4 book ai didi

Java geoip2 java.io.FileNotFoundException :

转载 作者:行者123 更新时间:2023-12-01 17:00:37 26 4
gpt4 key购买 nike

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

   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

最佳答案

你可以试试这个

InputStream is = this.getClass().getClassLoader().getResourceAsStream("GeoLite2-Country.mmdb");

关于Java geoip2 java.io.FileNotFoundException :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61509829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com