SpringBoot项目从resources目录读取文件

编程入门 行业动态 更新时间:2024-10-14 16:21:25

SpringBoot项目从resources目录读取<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件"/>

SpringBoot项目从resources目录读取文件

SpringBoot 从 resources 读取文件

使用 Spring 给我们提供的工具类来进行读取

File file = org.springframework.util.ResourceUtils.getFile("classpath:人物模板.docx");

可能读取失败,出现如下错误:

java.io.FileNotFoundException: class path resource [人物模板.docx] cannot be resolved to absolute file path because it does not exist

该问题是因为 resources/人物模板.docx 这个文件并没有被编译到 target/classes 目录下,所以需要在 pom.xml 中进行配置

根据上图发现 classes 中并没有我们需要的文件

在 pom 中添加如下配置:

<resources><resource><directory>src/main/resources</directory><includes><include>**/*.*</include></includes><filtering>false</filtering></resource>
</resources>

再次通过 maven 的 install 工具打包项目:

可以发现,target/classes 目录出现需要文件,此时就可以进行读取

更多推荐

SpringBoot项目从resources目录读取文件

本文发布于:2023-11-16 17:24:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   目录   项目   SpringBoot   resources

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!