关于java读取resource下文件,程序报错不存在问题的解决

先排除路径错误等低级错误。

一般读取某文件,比如在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> 注释掉。再次编译查看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值