springboot使用ResourceUtils.getFile读取不到文件
springboot 读取文件失败
·
1.问题描述
今天分享一个发版遇到问题,读取resource下文件时报错,但是本地环境启动时并不会报错,最终定位到代码:
File jsonFile = ResourceUtils.getFile("classpath:xxx.json");
String settingJson = FileUtils.readFileToString(jsonFile, "UTF-8");
报错信息:
java.io.FileNotFoundException: class path resource [xxx] cannot be resolved to absolute file path because it does not reside in the file system: jar:file/xxx/BOOT-INF/classes/xxxxx
2.解决方案
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
ClassPathResource classPathResource = new ClassPathResource("xxx.json");
String settingJson = IOUtils.toString(classPathResource.getInputStream(),"utf8");

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)