class path resource [test-es-index.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/opt/app.jar!/BOOT-INF/classes!/test-es-index.txt
spring boot 打包后 jar文件,执行jar 后,内部程序访问 jar 中的 test-es-index.txt 报错。
打印各种日志发现,都存在,使用的是 resource.getFile(),一直报 上面错误。
换成文件流读取就没有问题了
ClassPathResource resource = new ClassPathResource("test-es-index.txt");
if(!resource.exists()){
log.info("test-es-index.txt 文件不存在");
}else{
String mapping = IOUtils.toString(inputStream,"utf-8");;
}
IOUtils 使用的是 apache 工具包
import org.apache.commons.io.IOUtils;
本文探讨了SpringBoot项目中,从打好的Jar包内部读取资源文件时遇到的问题及解决方案。当使用ClassPathResource的getFile()方法时,会因文件不在文件系统中而报错。文章提供了通过InputStream方式读取的替代方案,并引入了Apache Commons IOUtils工具类。
9376

被折叠的 条评论
为什么被折叠?



