Bootstrap

spring boot 获取jar包中的资源

    public static void getJarResourceFile(String fileDir, String desDir) {

        File dir = new File(desDir + File.separator + fileDir);
        if (dir.exists() && dir.listFiles().length > 0)
            return;

        dir.mkdirs();

        //获取容器资源解析器
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            //获取所有匹配的文件
            Resource[] resources = resolver.getResources("classpath:" + fileDir + "/*.*");
            for (Resource src : resources) {
                try {
                    //获得文件流,因为在jar文件中,不能直接通过文件资源路径拿到文件,但是可以在jar包中拿到文件流
                    InputStream stream = src.getInputStream();

                    String targetFilePath = dir.getPath() + File.separator + src.getFilename();
                    File ttfFile = new File(targetFilePath);
                    Files.copy(stream, ttfFile.toPath());

                } catch (Exception e) {
                }
            }

        } catch (IOException e) {

        }

    }

 

;