java - 列出 JAR 资源文件夹中所有子目录的名称

标签 java spring-boot jar apache-commons

我使用 maven 将我的 Spring Boot 项目打包到一个 JAR 中。我想读取资源文件夹中“static/foo”下所有目录的名称。

使用 Apache Commons 库尝试了以下操作:

String fooPath = "static/foo/";
List<String> fooFolders = IOUtils.readLines(this.getClass().getClassLoader().getResourceAsStream(fooPath), Charsets.UTF_8);
// The fooFolders list is empty ...

更新

此解决方案使用 JarFile 有效(从 https://stackoverflow.com/a/48190582/1427624 稍微修改):

String fooPath = "static/foo";
URL url = Thread.currentThread().getContextClassLoader().getResource(fooPath);

// Not running from JAR
if (url.getProtocol().equals("file"))
{
    try {
        // Get list of subdirectories' folder names
        List<String> fooFolders = IOUtils.readLines(this.getClass().getClassLoader().getResourceAsStream(fooPath), Charsets.UTF_8);

        // Loop subdirectories
        for (String fooFolder : fooFolders) {
            // The current subdirectory path
            String fooFolderPath = fooPath + "/" + fooFolder;

            // Loop all files in this subdirectory, if needed
            List<String> fooFiles = IOUtils.readLines(this.getClass().getClassLoader().getResourceAsStream(fooFolderPath), Charsets.UTF_8);

            for (String fooFile : fooFiles) {
                // The updated path of the file
                String fooFilePath = fooFolderPath + "/" + fooFile;

                // Read the file's content
                InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fooFilePath);
                StringWriter writer = new StringWriter();
                IOUtils.copy(inputStream, writer, Charsets.UTF_8);
                String fileContent = writer.toString();
            }
        }
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

// Running from JAR
else if (url.getProtocol().equals("jar")) {
    String dirname = fooPath + "/";
    String path = url.getPath();
    String jarPath = path.substring(5, path.indexOf("!"));

    List<String> fooFolders = new ArrayList<String>();
    HashMap<String, List<String>> fooFiles = new HashMap<String, List<String>>();

    try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            String jarEntryName = entry.getName();

            String updated_dir_name = "BOOT-INF/classes/" + dirname;

            // Only get files that are in the directory we require (fooPath)
            if (jarEntryName.startsWith(updated_dir_name) && !dirname.equals(updated_dir_name)) {

                // Get the resource URL
                URL resourceURL = Thread.currentThread().getContextClassLoader().getResource(jarEntryName);

                // Files only
                if (!jarEntryName.endsWith("/")) {

                    // Split the foo number and the file name
                    String[] split = jarEntryName.split("/");

                    // First level subdirectories inside fooPath
                    // BOOT-INF/classes/static/foo/1/myfile.html
                    // We want to read the folder name "1"
                    String folderName = split[split.length - 2];

                    // If you want to read this file
                    // Read the file's content
                    // InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceURL);
                }
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

最佳答案

这听起来可以通过 https://stackoverflow.com/a/3923685/7610371 来解决

上述答案中的“核心”部分:

InputStream resourceStream = this.getClass().getClassLoader().getResourceAsStream(fooPath);
BufferedReader br = new BufferedReader(new InputStreamReader(resourceStream)) 
String resource;

while ((resource = br.readLine()) != null) {
    // ... resource is the next filename; you can add it to an array
    // or use it here
}

关于java - 列出 JAR 资源文件夹中所有子目录的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54888723/

相关文章:

java - 如何更改文本字段的值 + jQuery + Struts2

java - 暂停/恢复线程

java - 如何在 Spring Boot 中强制刷新 OutputStream/Writer

spring-mvc - 为特定领域设计的 Spring Boot Web 服务

Ant脚本生成的Java Jar不支持用户输入

java - 当我从 jar 运行应用程序时,为什么 new File(path) 不起作用?

java - 将resteasy-spring模块加载到WildFly ClassNotFoundException中

java - Oracle数据库: access multiple database schemas

java - 无法使用spring boot将数据保存到数据库

java - jar 名称中的版本号 - 如何处理 persistence.xml?