Java怎么读取jar包中的resource资源文件

发布时间:2022-08-27 09:25:56 作者:iii
来源:亿速云 阅读:405

本篇内容介绍了“Java怎么读取jar包中的resource资源文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1、需求       

在Java项目中,需要读取resource资源目录下的文件,以及遍历指定资源目录下的所有文件,并且在读取文件时保留文件相对路径。

2、问题       

在IDEA中运行时,可以获取并遍历指定资源,但是将Java项目打成jar包运行后,就无法获取resource资源目录下的文件。

3、IDEA读取resource资源       

编译后,资源文件放在target目录下,每一个资源文件实实在在存在于磁盘中。

3.1、方法1       

直接通过绝对路径读取,如果file是目录,也可以通过listFiles递归遍历目录下文件:

String absolutePath = "资源文件绝对路径";
File file = new File(absolutePath);
if (file.isDirectory()) {
    File[] children = file.listFiles();
}

3.2、方法2       

通过相对路径读取:

String path = "template";    //相对resource路径
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + path);
if (file.isDirectory()) {
    File[] children = file.listFiles();
}

4、打成jar包后读取resource资源       

以上两种方法无法读取jar包中的资源文件。

打成jar包后,jar包是一个单独的文件而不是文件夹,所以通过文件路径是无法定位到资源文件的。此时,可通过类加载器读取jar包中的资源文件。

4.1、读取jar包中的资源文件       

这种方式只能读取jar包中单个文件,因为读取出来的是InputStream流,无法保留文件相对于resource的路径,所以无法对jar包中资源进行遍历。

String path = "/resource相对路径";
InputStream is = this.class.getResourceAsStream(path);
byte[] buff = new byte[1024];
String filePath = "保存文件路径";
String fileName = "保存文件名";
File file = new File(filePath + fileName);
FileUtils.copyInputStreamToFile(is, file);

4.2、遍历jar包资源目录

以复制resource资源目录为例,分别对本地和jar包中的资源进行复制。

如下所示:

我要复制resource资源目录下的template文件夹下的所有内容;

然后保存到C:/Users/ASUS/Desktop/savePath文件夹下。

4.2.1、环境判断

public static void main(String[] args) throws URISyntaxException {
    // Test为当前类名
    URI uri = Test.class.getProtectionDomain().getCodeSource().getLocation().toURI();
    // tempPath: 文件保存路径
    String tempPath = "C:/Users/ASUS/Desktop/savePath";
    String sourceDir = "template";  //资源文件夹
    if (uri.toString().startsWith("file")) {
        // IDEA运行时,进行资源复制
        copyLocalResourcesFileToTemp(sourceDir + "/", "*", tempPath + "/" + sourceDir);
    } else {
        // 获取jar包所在路径
        String jarPath = uri.toString();
        uri = URI.create(jarPath.substring(jarPath.indexOf("file:"),jarPath.indexOf(".jar") + 4));
        // 打成jar包后,进行资源复制
        Test.copyJarResourcesFileToTemp(uri, tempPath, "BOOT-INF/classes/" + sourceDir);
    }
}

4.2.2、复制本地项目的资源文件

/**
     * 复制本地资源文件到指定目录
     * @param fileRoot      需要复制的资源目录文件夹
     * @param regExpStr     资源文件匹配正则,*表示匹配所有
     * @param tempParent    保存地址
     */
    public static void copyLocalResourcesFileToTemp(String fileRoot, String regExpStr, String tempParent) {
        try {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resolver.getResources(fileRoot + regExpStr);
            for (Resource resource : resources) {
                File newFile = new File(tempParent, resource.getFilename());
                if (newFile.exists()) {
                    newFile.delete();
                }
                InputStream stream = null;
                try {
                    stream = resource.getInputStream();
                } catch (Exception e) {
                    // 如果resource为文件夹时,会报异常,这里直接忽略这个异常
                }
                if (stream == null) {
                    newFile.mkdirs();
                    copyLocalResourcesFileToTemp(fileRoot + resource.getFilename()  + "/", regExpStr, tempParent + "/" + resource.getFilename());
                } else {
                    if (!newFile.getParentFile().exists()) {
                        newFile.getParentFile().mkdirs();
                    }
                    org.apache.commons.io.FileUtils.copyInputStreamToFile(stream, newFile);
                }
            }
        } catch (Exception e) {
            log.error("failed to copy local source template", e);
        }
    }

4.2.3、复制jar包里的资源文件

/**
     * 复制jar包中的资源文件到指定目录
     * @param path          jar包所在路径
     * @param tempPath      保存目录
     * @param filePrefix    需要进行复制的资源文件目录:以BOOT-INF/classes/开头
     */
    public static void copyJarResourcesFileToTemp(URI path, String tempPath, String filePrefix) {
        try {
            List<Map.Entry<ZipEntry, InputStream>> collect =
                    readJarFile(new JarFile(path.getPath()), filePrefix).collect(Collectors.toList());
            for (Map.Entry<ZipEntry, InputStream> entry : collect) {
                // 文件相对路径
                String key = entry.getKey().getName();
                // 文件流
                InputStream stream = entry.getValue();
                File newFile = new File(tempPath + key.replaceAll("BOOT-INF/classes", ""));
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
                org.apache.commons.io.FileUtils.copyInputStreamToFile(stream, newFile);
            }
        } catch (IOException e) {
            log.error("failed to copy jar source template", e);
        }
    }
@SneakyThrows
    public static Stream<Map.Entry<ZipEntry, InputStream>> readJarFile(JarFile jarFile, String prefix) {
        Stream<Map.Entry<ZipEntry, InputStream>> readingStream =
                jarFile.stream().filter(entry -> !entry.isDirectory() && entry.getName().startsWith(prefix))
                        .map(entry -> {
                            try {
                                return new AbstractMap.SimpleEntry<>(entry, jarFile.getInputStream(entry));
                            } catch (IOException e) {
                                return new AbstractMap.SimpleEntry<>(entry, null);
                            }
                        });
        return readingStream.onClose(() -> {
            try {
                jarFile.close();
            } catch (IOException e) {
                log.error("failed to close jarFile", e);
            }
        });
    }

“Java怎么读取jar包中的resource资源文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. SpringBoot如何读取war包jar包和Resource资源
  2. Android中怎么读取资源文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java jar resource

上一篇:C++智能指针怎么创建和使用

下一篇:Vs-code/WebStorm中如何构建Vue项目

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》