从不同 folder/classpath 加载配置文件
Load in config file from different folder/classpath
我目前正在开发 REST API,它需要从位于不同 folder/classpath.
中的配置文件中加载 属性
路径看起来像这样,
休息 class: mainfolder/folder1/src/main/java/folder2/rest/rest.java
配置文件:mainfolder/folder3/props.conf
现在,我的代码是这样的:
@GET
@Path("backups")
@Produces(MediaType.APPLICATION_JSON)
public List<FileInfo> getBackups(){
String localStorage= "D:/Backup";
Util util = new Util();
try {
Properties configFile = new java.util.Properties();
final InputStream cfg = new FileInputStream("folder3/props.conf");
try {
configFile.load(cfg);
localStorage = configFile.getProperty(FTPService.FTP_DOWNLOAD_TARGET);
return util.listBackupFilesInLocalDir(localStorage);
} finally {
cfg.close();
}
}catch (Exception e){
System.out.println(e);
}
return util.listBackupFilesInLocalDir(localStorage);
}
现在,我收到错误 500,这是因为在 FileInputStream
中找不到 "folder/props.conf"
?当我在我的系统上有绝对文件路径时它起作用了,但由于系统不在我的计算机上,所以我需要能够在文件系统中的任何位置获取文件。这可能吗?
好的,我在测试时非常简单,我只是输入 FileInputStream“../folder3/props.conf”,它找到了!
我目前正在开发 REST API,它需要从位于不同 folder/classpath.
中的配置文件中加载 属性路径看起来像这样,
休息 class: mainfolder/folder1/src/main/java/folder2/rest/rest.java
配置文件:mainfolder/folder3/props.conf
现在,我的代码是这样的:
@GET
@Path("backups")
@Produces(MediaType.APPLICATION_JSON)
public List<FileInfo> getBackups(){
String localStorage= "D:/Backup";
Util util = new Util();
try {
Properties configFile = new java.util.Properties();
final InputStream cfg = new FileInputStream("folder3/props.conf");
try {
configFile.load(cfg);
localStorage = configFile.getProperty(FTPService.FTP_DOWNLOAD_TARGET);
return util.listBackupFilesInLocalDir(localStorage);
} finally {
cfg.close();
}
}catch (Exception e){
System.out.println(e);
}
return util.listBackupFilesInLocalDir(localStorage);
}
现在,我收到错误 500,这是因为在 FileInputStream
中找不到 "folder/props.conf"
?当我在我的系统上有绝对文件路径时它起作用了,但由于系统不在我的计算机上,所以我需要能够在文件系统中的任何位置获取文件。这可能吗?
好的,我在测试时非常简单,我只是输入 FileInputStream“../folder3/props.conf”,它找到了!