最近做项目遇到一个坑,在Resources文件配置SDK初始化参数,本地运行可以读取到文件内容,但是打包部署到服务器上就会出现读不到资源文件的情况,这里记录一下。下图是资源文件放置目录:
一开始读取代码如下:
Properties properties = new Properties();
FileInputStream fileInputStream = null;
try {
String path = OnlinepayApplication.class.getClassLoader().getResource("").getPath();
fileInputStream = new FileInputStream(path + "drippay.properties");
properties.load(fileInputStream);
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这种读取方式是从程序编译生成的target文件家目录下找资源文件,下图是编译生成的target目录: