先排除路径错误等低级错误。
一般读取某文件,比如在resource目录下的ehcache文件夹中有ehcache-shiro.xml文件,使用以下方式读取
ResourceUtils.getInputStreamForPath(“classpath:ehcache/ehcache-shiro.xml”);
代码中要思考,classpath意义。简明之意是,告诉程序到编译后的路径下去读取文件,这一点通过看源码可以理解。
如果出现了原本有文件,但仍报错不存在,可以从编译后的路径中,查看resource下文件是否全部被编译到输出路径。
如果发现编译数据路径下并没有resource包含文件的存在,那么问题就找到了。
以我项目为例,springboot及maven。
1、在pom文件中,指定编译资源。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>/statics</directory>
<excludes>
<exclude>**/*.woff</exclude>
<exclude>**/*.woff2</exclude>
<exclude>**/*.ttf</exclude>
<exclude>**/*.eot</exclude>
<exclude>**/*.svg</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
2、若仍有问题,尝试将pom文件中 <packaging>pom</packaging> 注释掉。再次编译查看。