我在 Spring Boot 应用程序的 application.yml 文件中给出了下面
文件.路径:类路径:input.txt
我已将 input.txt 放在 src/main/resources 下
当我在 docker 中部署应用程序时,出现以下异常
由以下原因引起:java.io.FileNotFoundException:类路径资源 [input.txt] 无法解析为绝对文件路径,因为它不驻留在文件系统中:jar:file:/app/app.jar !/BOOT-INF/classes!/input.txt
但是当我从本地运行它时,文件正在加载。请建议一种修复方法。
最佳答案
根据您的回答,我注意到您正在加载这样的文件:
@Value("${file.path}") //classpath:input.txt
private String filePath;
private void readFile() {
new File(filePath);
}
要使其正常工作,您可以像这样使用它:
@Value("${file.path}")
private org.springframework.core.io.Resource file;
private void readFile() {
InputStream is = file.getInputStream();
// your code goes here
}
关于java - Spring Boot 应用程序无法检测类路径中存在的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58839144/