繁体   English   中英

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

[英]Spring Boot application is not able to detect the file present in classpath

我在 spring 启动应用程序的 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

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

根据您的回答,我注意到您正在加载这样的文件:

@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
}

您必须使用 Spring 中的以下代码读取文件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM