美文网首页
springboot获取resources下的文件失败解决记录

springboot获取resources下的文件失败解决记录

作者: len_清水白 | 来源:发表于2019-06-23 23:30 被阅读0次

今天在做excel导出功能的时候,发现老是获取不到resources下的excel文件。报错:

java.io.FileNotFoundException: class path resource [templates/excelTemplate.xlsx] cannot be resolved to URL because it does not exist

一开始以为是我写的用来获取文件的方法有问题,然后各种百度,用了好多种方法都不行(还是对源码不了解啊)。突然想到这些文件都要先编译到classpatch下的,它找不到我的文件是不是因为没编译呢?果然,classes下除了一些application.yml文件,其他文件都没编译过来。那为什么呢?

百度后总结:maven构建项目的时候不会自动的把resources下的文件都编译,你需要告诉maven你需要编译什么文件。

代码如下:

  <finalName>recruit-service

        <groupId>org.springframework.boot

        <artifactId>spring-boot-maven-plugin

            <fork>true

        <directory>src/main/java

            <include>**/*.xml

        <directory>src/main/resources

            <include>**/*.yml

            <include>**/*.xlsx

</build>

相关文章

网友评论

      本文标题:springboot获取resources下的文件失败解决记录

      本文链接:https://www.haomeiwen.com/subject/olkgqctx.html