After sprinboot packs the jar, it cannot read the file of /resource/data/ip2region.xdb.
Solution 1: Copy the file outside the project and access it directly with the path.
Solution 2: If the file must be included in the project, the package must be included, and the code can be copied from the jar package to the server.
1: Maven package needs to add your file
<build>
<finalName>${project.artifactId}</finalName >
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.xml</include>
<include>*.txt</include>
<include>*.properties</include> include>
<include>*.yml</include>
<include>mapper/*.xml</include>
<include>static/**</include>
<include>data/ip2region.xdb</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
2: After packaging, the file is in the jar package, not in your directory. You can copy it from the jar package. //The
root path where the project is packaged into a jar package
String rootPath = applicationHome.getSource().getParentFile().toString( );
String configFilePath = rootPath + "/data/ip2region.xdb";
File configFile = new File(configFilePath);
if (!configFile.exists()) { try { //Get the specified file stream under the class path (project directory under: /resource/data/ip2region.xdb) InputStream in = this.getClass().getClassLoader().getResourceAsStream("data/ip2region.xdb"); FileUtils.copyInputStreamToFile(Objects.requireNonNull(in, "/data/ ip2region.xdb file not found"), configFile); } catch (IOException e) { //throw new IllegalArgumentException("Failed to save file certificate->" + e.getMessage());
log.info("Save file data file ip2region failed ({}): {}", ip, e); } }
log.info
(
"Data file ip2region.xdb address: {}", configFilePath);