Spring Boot即使在代码中使用资源加载而不是文件位置,也不会在资源文件夹中找到文件
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论