简体   繁体   English

来自src / main / resources / somefolder的JSF应用程序列表文件

[英]JSF Application list files from src/main/resources/somefolder

I'm trying to get a list of files from a folder in the src/main/resources directory of my webapp. 我正在尝试从Web应用程序的src / main / resources目录中的文件夹中获取文件列表。 Does anyone have a solution ? 有没有人有办法解决吗 ? So, one problem is, that I have only the glassfish.jar in my classpath. 因此,一个问题是,我的类路径中只有glassfish.jar。 I don't find an entrypoint from where I can start. 我找不到可以从哪里开始的切入点。 It's a maven project. 这是一个行家项目。

Regards, Holger 问候,霍尔格

The solution so far : which works, but doesn't satisfy me, because of the absolute path. 到目前为止的解决方案:可行,但由于绝对路径,我不满意。

org.apache.commons.io.FileUtils has to be included. 必须包含org.apache.commons.io.FileUtils。

public Collection<File> queryResourceFileCollectionByDirectoryMember(Class clazz, String resourcePathToMember, String[] extensions) {
    logger.info("------- FileTools : queryResourceFileCollectionByDirectoryMember");

    try {
        //String[] extensions = {"xml"};
        URL resourceURL = clazz.getResource(resourcePathToMember);//"/fields/testOITM.xml" <- one XML file in the /src/main/resources/fields/ folder to get the absolutepath which after deployment is WEB-INF/classes/fields
        String resourcePath = resourceURL.getPath();

        //Remove the leading slash
        String resourcePathWithoutFirstChar = resourcePath.substring(1);
        logger.log(Level.INFO, "------- FileTools : queryResourceFileCollectionByDirectoryMember: resourcePath :{0} :", resourcePathWithoutFirstChar);
        File resourceFileToGetDirectory = new File(resourcePathWithoutFirstChar);
        String resourceDirectoryPath = resourceFileToGetDirectory.getParent() + File.separator; //"c:/Users/xuser/Documents/NetBeansProjects/BOneBApp/target/classes/"
        File resourceDirectory = new File(resourceDirectoryPath);
        Collection<File> files = FileUtils.listFiles(resourceDirectory, extensions, true);
        return files;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "******* FileTools : queryResourceFileCollectionByDirectoryMember : {0}", e.getMessage());
        return null;
    }
}

The clazz can be any class of the webapp. 风格可以是webapp的任何类别。

Regards, Holger 问候,霍尔格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问存储在 Docker 容器中 'src/main/resources/someFolder' 中的应用程序资源文件? - How to access app resource files stored in 'src/main/resources/someFolder' within a Docker container? 应用程序(src / main)运行时,类路径中的src / test / resources中的文件是? - Are files in src/test/resources on classpath while application (src/main) is running? 从Java中的src / main / resources访问静态文件 - Access static files from src/main/resources in Java 无法从src / main / resources文件夹Java 8中读取文件 - Cant read files from src/main/resources folder Java 8 JavaEE Web-从/ src / main / resources访问文件 - JavaEE web - accessing files from /src/main/resources 无法在Spring引导应用程序中将src / main / resources / static / css /文件夹中的css文件提供给客户端 - Unable to serve css files placed in src/main/resources/static/css/ folder to client in Spring boot application 从src / main / resources读取会产生NullPointerException - Reading from src/main/resources gives NullPointerException 如何在运行时从JSF控制器访问位于“ src / main / resources”文件夹中的css文件 - How can I access a css file located in my “src/main/resources” folder from my JSF controller at runtime 从/ src / main / resources加载文件 - Loading file from /src/main/resources 如何包含来自 src/main/java 的资源 - How to include resources from src/main/java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM