【问题标题】:What should be path for the files that are in resources folder in spring bootspring boot中资源文件夹中文件的路径应该是什么
【发布时间】:2018-12-19 09:23:56
【问题描述】:

我想访问证书文件,如果该文件位于 resources/certs/ 文件夹中,路径应该是什么。

我用classpath 尝试过,仍然得到异常FileNotFound

我应该在我的 application.properties 中为此指定什么路径。

【问题讨论】:

  • classpath:certs/teye-feeds.jks 是正确的,只要你的 Maven 使用类路径中的 src/main/resources 的内容构建包即可。
  • 代码结构是基于maven的,你可以看到src/main/resource
  • 我可以看到。我看不到的是 Maven 是如何构建你的包的。我假设您会遵循可执行 JAR 或 WAR 文件的约定。

标签: java spring-boot configuration


【解决方案1】:

如果你想读取一个文件,你可以使用这个代码(文件名:“/certs/xxx”)

public String readFile(String fileName) throws IOException {
        InputStream inputStream = this.getClass().getResourceAsStream(fileName);
        StringBuilder resultStringBuilder = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = br.readLine()) != null) {
                resultStringBuilder.append(line).append("\n");
            }
        }
        return resultStringBuilder.toString();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 2011-08-25
    • 2017-11-08
    • 2011-03-07
    • 2010-12-02
    • 2017-03-13
    相关资源
    最近更新 更多