简体   繁体   English

从不同的文件夹/类路径加载配置文件

[英]Load in config file from different folder/classpath

I am currently working on a REST API that need to load in a property from a config file that is in a different folder/classpath. 我目前正在研究REST API,该API需要从其他文件夹/类路径中的配置文件中加载属性。

The path looks like this, 路径看起来像这样,

rest class: mainfolder/folder1/src/main/java/folder2/rest/rest.java 休息类:mainfolder / folder1 / src / main / java / folder2 / rest / rest.java

config file: mainfolder/folder3/props.conf 配置文件:mainfolder / folder3 / props.conf

Right now, the code I have is this: 现在,我的代码是这样的:

@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);
}

Right now, I get an error 500 and it is because it can't find the "folder/props.conf" in the FileInputStream ? 现在,我得到一个错误500,这是因为它在FileInputStream找不到"folder/props.conf" It worked when I had the absolute file path on my system in there but since the system when its live ain't on my computer I need to be able to get the file wherver it is in the file system. 当我的系统中有绝对文件路径时,它就可以工作,但是由于该系统不在我的计算机上,因此我需要能够将文件保存在文件系统中。 Is this possible? 这可能吗?

好的,在我测试时非常简单,我只将FileInputStream放在“ ../folder3/props.conf”中,就可以找到它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM