Spring Boot应用程序无法检测到类路径中存在的文件

问题描述 投票:0回答:1

我在春季启动应用程序的application.yml文件中提供了以下内容

file.path: classpath:input.txt

我已将input.txt放在src / main / resources下

当我在Docker中部署应用程序时,出现以下异常

Caused by: java.io.FileNotFoundException: class path resource [input.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app/app.jar!/BOOT-INF/classes!/input.txt

但是当我从本地运行它时,文件正在加载。请提出一种解决方法。

java spring-boot docker
1个回答
0
投票
您必须在Spring中使用以下代码读取文件。

File rese = new ClassPathResource("input.txt").getFile(); String contents = new String(Files.readAllBytes(res.toPath()));

© www.soinside.com 2019 - 2024. All rights reserved.