SpringBoot:cannot be resolved to absolute file path because it does not reside in the file system

该博客聚焦Spring Boot项目,在本地运行读取文件正常,但打包部署后读取文件报错。原因是打包后项目从jar包而非文件系统读取文件,解决方式是从jar中读取文件流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot项目里要读取项目中的文件内容,本地运行时正常,打包部署读取文件时报错

cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/xxxx-1.0.0-SNAPSHOT.jar!/BOOT-INF/classes!/data/123.txt

报错代码:

File file = ResourceUtils.getFile("classpath:data/123.txt");
String fileContent = FileUtil.readString(file, StandardCharsets.UTF_8);

报错原因:

打包后,项目将从jar包中,而不是从文件系统中读取文件。

解决方式:

从jar中读取文件流

ClassPathResource resource = new ClassPathResource("data/123.txt");
InputStream inputStream = resource.getInputStream();
String fileContent = IOUtils.toString(inputStream, "utf-8");

完结撒花✿✿ヽ(°▽°)ノ✿

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值