跳至主要內容

SpringBoot读取jar中resource下的文件

Jin大约 2 分钟

SpringBoot读取jar中resource下的文件

    /**
     * <h2>获取jar包文件名称集合</h2>
     * @return
     */
    public static List<String> getFileNamesByJar(String dirPath){
        List<String> list = new ArrayList<>();
        try {
            Resource[] resources = new PathMatchingResourcePatternResolver().
                    getResources(ResourceUtils.CLASSPATH_URL_PREFIX + dirPath + "**");
            // 遍历文件内容
            for(Resource resource : resources) {
                String filename = resource.getFilename();
                if(StringUtils.isBlank(filename)) {
                    continue;
                }
                list.add(filename);
            }
        } catch (IOException e) {
            log.error("获取jar包文件名称集合失败", e);
        }
        return list;
    }

其他读取

public static void main(String[] args) throws IOException {
        // TODO 加载当前项目classpath下META-INF/folder及其子文件夹中的所有文件
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(ResourceUtils.CLASSPATH_URL_PREFIX + "META-INF/folder/**/*.txt");
 
        // TODO 加载当前项目classpath下META-INF/folder及其子文件夹中的所有以.txt结尾的文件
        Resource[] resources2 = new PathMatchingResourcePatternResolver().getResources(ResourceUtils.CLASSPATH_URL_PREFIX + "META-INF/folder/**/*.txt");
 
        // TODO 加载当前项目及所有jar中classpath下的所有META-INF/spring.factories文件(springboot自动装配的主要功能)
        Resource[] resources3 = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "META-INF/spring.factories");
 
        // 遍历文件内容
        for(Resource resource : resources) {
            StringBuffer script = new StringBuffer();
            try(InputStreamReader isr = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8);
                BufferedReader bufferReader = new BufferedReader(isr)) {
                String tempString;
                while ((tempString = bufferReader.readLine()) != null) {
                    script.append(tempString).append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("script:" + script.toString());
        }
    }

springboot 获取类路径方式集合

//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