【问题标题】:Load in config file from different folder/classpath从不同的文件夹/类路径加载配置文件
【发布时间】:2018-03-21 16:22:53
【问题描述】:

我目前正在开发一个 REST API,它需要从位于不同文件夹/类路径中的配置文件加载属性。

路径是这样的,

休息类: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"?当我的系统上有绝对文件路径时它起作用了,但是由于系统不在我的计算机上,所以我需要能够在文件系统中获取文件。这可能吗?

【问题讨论】:

  • '获取文件系统中的文件'。 在哪里文件?它在类路径上吗?

标签: java rest config


【解决方案1】:

好的,这很简单,当我在测试时,我只是把 FileInputStream "../folder3/props.conf" 放进去,它就找到了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2019-01-30
    • 2016-06-09
    • 1970-01-01
    相关资源
    最近更新 更多