[英]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.