先说结论

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

需要注意的是,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;
    }


}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.

模板文件位置

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

m1_2文件位置

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