跳至主要內容

SpringBoot读取jar中resource下的文件

Jin大约 1 分钟

SpringBoot读取jar中resource下的文件

1、SpringBoot读取jar中resource下的文件

方法一

String resourcePath1 = "/item/img/";
     try {
            ClassPathResource resource = new ClassPathResource("static" + resourcePath);
            File file = resource.getFile();
            logger.info(file.toString());
            if(file != null && file.isDirectory()){
                File[] files = file.listFiles();
                if(files != null && files.length >0){
                    for (File f:files) {
                        logger.info(f.getName());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("读取异常",e.getStackTrace());

        }

方法二

String resourcePath = "/item/img/";
    try {
            ClassPathResource resource = new ClassPathResource("static" + resourcePath);
            InputStream inputStream = resource.getInputStream();
            logger.info("=====================开始========================");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while((line = bufferedReader.readLine()) != null){
                logger.info(line);
            }
            logger.info("=======================结束======================");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("读取异常",e.getStackTrace());

        }

首先,这俩种方法在idea中运行时没有问题,可以读取到文件;

而且第二种方法在jar中也可以读取指定的文件,地址必须是具体到文件名,读取目录还是不行

2、获取类路径方式集合

//spring boot获取类路径 获取当前类路径
        String springbooPath1 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        System.out.println("springbooPath1:"+springbooPath1);
        String springbooPath2 = ResourceUtils.getURL("classpath:").getPath();
        System.out.println("springbooPath2:"+springbooPath2);
        //不能这样写
       // String springbooPath3 = ClassUtils.getDefaultClassLoader().getResource("/").getPath();

        //环境变量中的属性
        Properties properties = System.getProperties();
        //jar包所在的路径
        String dir = properties.getProperty("user.dir");
        System.out.println("dir:"+dir);
        String realPath = properties.getProperty("realPath");
        System.out.println("realPath:"+realPath);
        String jarWholePath = properties.getProperty("jarWholePath");
        System.out.println("jarWholePath:"+jarWholePath);

        String protectionDomain = MonitorController.class.getProtectionDomain().getCodeSource().getLocation().getFile();
        System.out.println("protectionDomain:"+protectionDomain);

        //class 获取方式 获取当前类文件所在路径
        String classPath = this.getClass().getResource("").getPath();
        System.out.println("classPath:"+classPath);
        //class 获取方式 获取当前类路径
        String classPath2 = this.getClass().getResource("/").getPath();
        System.out.println("classPath2:"+classPath2);

        //通过类加载器获取jar包的绝对路径
        String classLoaderPath = MonitorController.class.getClassLoader().getResource("").getPath();
        System.out.println("classLoaderPath:"+classLoaderPath);
        //不能这样写
//        String classLoaderPath2 = MonitorController.class.getClassLoader().getResource("/").getFile();

        URL contextClassPath1 = Thread.currentThread().getContextClassLoader().getResource("");
        System.out.println("contextClassPath1:"+contextClassPath1.getPath());
        //不能这样写
//        URL contextClassPath2 = Thread.currentThread().getContextClassLoader().getResource("/");

贡献者: Jin