spring boot ResourceUtil工具类获取不到jar的classPath下面文件问题

发布时间 2023-06-10 10:22:20作者: zhangyukun

先说结论

  1. 这种方式是获取不到jar里面的文件的,但是在本地可以跑,打成jar包就读取不到文件了,即便 ResourceUtils 是spring提供的。
File file = ResourceUtils.getFile("classpath:temp\\file\\info.txt");
  1. spring提供的ClassPathResource对象是可以在本地和jar环境都获取到资源的
ClassPathResource classPathResource = new ClassPathResource("temp\\file\\info.txt");
InputStream inputStream=classPathResource.getInputStream();
  1. 另外java原生的方式也可以一获取到jar里面的文件
InputStream inputStream = getClass().getResourceAsStream("/temp/file/info.txt");

需要注意的是,getClass().getResourceAsStream 默认是取当前包名下面的资源,如果是/开头是取classpath下面的资源

测试代码如下:

package com.lomi.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.lomi.annotation.ShowParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

@Api(tags = "文件读取")
@RestController
@RequestMapping(value = "/file")
@Slf4j
public class FileController extends BaseController {


    /**
     * 本地运行,m1不能获取到文件,m1_1,m2,m3可以
     * jar包以后运行:m1,m3不能获取到文件,m1_1,m2可以
     * M1_2 是相对路径放在指定的路径下也可以jar和运行时都读取到
     *
     * @return
     * @throws IOException
     */


    @ApiOperation(value = "读取本地classpath下面的文件方式1")
    @GetMapping("/readFile/m1")
    @ShowParam
    public String readFileM1() throws IOException {
        //方法1,不读取jar里面的文件
        InputStream inputStream = getClass().getResourceAsStream("temp\\file\\info.txt");

        String content = IoUtil.readUtf8(inputStream);
        log.debug("内容是:{}", content);
        return content;
    }


    /**
     * getClass().getResourceAsStream 默认是取当前包名下面的资源,如果是/开头是取classpath下面的资源
     *
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "读取本地classpath下面的文件方式1_1")
    @GetMapping("/readFile/m1_1")
    @ShowParam
    public String readFileM1_1() throws IOException {
        //方法1,不读取jar里面的文件
        InputStream inputStream = getClass().getResourceAsStream("/temp/file/info.txt");

        String content = IoUtil.readUtf8(inputStream);
        log.debug("内容是:{}", content);
        return content;
    }



    /**
     * getClass().getResourceAsStream 默认是取当前包名下面的资源(com.lomi.controller),如果是/开头是取classpath下面的资源
     *
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "读取本地classpath下面的文件方式1_2")
    @GetMapping("/readFile/m1_2")
    @ShowParam
    public String readFileM1_2() throws IOException {
        //方法1,不读取jar里面的文件
        InputStream inputStream = getClass().getResourceAsStream("info.txt");

        String content = IoUtil.readUtf8(inputStream);
        log.debug("内容是:{}", content);
        return content;
    }



    @ApiOperation(value = "读取本地classpath下面的文件方式2")
    @GetMapping("/readFile/m2")
    @ShowParam
    public String readFileM2() throws IOException {
        //方法2,可以读取打成jar包里面的文件
        ClassPathResource classPathResource = new ClassPathResource("temp\\file\\info.txt");
        InputStream inputStream = classPathResource.getInputStream();


        String content = IoUtil.readUtf8(inputStream);
        log.debug("内容是:{}", content);
        return content;
    }

    @ApiOperation(value = "读取本地classpath下面的文件方式3")
    @GetMapping("/readFile/m3")
    @ShowParam
    public String readFileM3() throws IOException {
        //方法3,可以读取打成jar包里面的文件
        File file = ResourceUtils.getFile("classpath:temp\\file\\info.txt");

        String content = FileUtil.readUtf8String(file);
        log.debug("内容是:{}", content);
        return content;
    }


}

模板文件位置

image-20230606182235946

m1_2文件位置
image-20230608101519183