Spring Boot即使在代码中使用资源加载而不是文件位置,也不会在资源文件夹中找到文件

发布于 2025-01-21 06:24:04 字数 799 浏览 3 评论 0原文

我正在使用Google的常见IO,库来读取Spring Boot应用程序资源文件夹中的文件。它在Intellij中的工作正常,但是当我从命令行运行此操作时,我的应用程序正在选择application.yml中存在的中存在 resources>资源文件夹,但没有选择。其他文件夹中的其他文件。

我已经检查了JAR文件中存在的文件,如下所示,

unzip -l my.jar | grep ".json"

 983  04-14-2022 16:16   BOOT-INF/classes/abc.json
 423  04-14-2022 16:16   BOOT-INF/classes/xyz.json

但是应用程序在未发现的情况下抛出以下文件,并且位置相同。

java.io.filenotfoundexception: file:/users/amit/code/so-search/target/my.jar!/boot-inf/class!/abc.json (没有这样的文件或目录)

这就是我在代码中读取文件的方式。

public static FileReader getFileReader(String fileName) throws FileNotFoundException {
        File file = new File(Resources.getResource(fileName).getFile());
        return new FileReader(file);
    }

I am using the google's common IO, library to read the files in my resources folder of Spring boot app. its working perfectly fine in the IntelliJ, but when I am running this from the command line, My app is picking the application.yml present in the same resources folder but not picking other files in the same folder.

I've checked that files are present in the jar file, as shown below

unzip -l my.jar | grep ".json"

 983  04-14-2022 16:16   BOOT-INF/classes/abc.json
 423  04-14-2022 16:16   BOOT-INF/classes/xyz.json

But App is throwing following File not found exception, with the same location.

java.io.FileNotFoundException:
file:/Users/amit/code/so-search/target/my.jar!/BOOT-INF/classes!/abc.json
(No such file or directory)

This is how I am reading the files in my code.

public static FileReader getFileReader(String fileName) throws FileNotFoundException {
        File file = new File(Resources.getResource(fileName).getFile());
        return new FileReader(file);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文