简体   繁体   中英

List all files inside resource directory Jersey servlet

I'm trying to retrieve all the files inside a resources folder to be able to use it on JAXB unmarshaller, but i could'nt find anything that i could use instead of the regular file.listFiles() on java.

I've tried File files = new File(this.context.getResource("/WEB-INF/folder").getPath()); and use it on a foreach loop, but a null pointer was thrown.

Just to point it out, context is a Jersey @Context ServletContext context injected variable.

@Update

Ok, im using getResourcePaths now and converting each path to uri and than to file.

Set<String> catalogosPath = this.context.getResourcePaths("/WEB-INF/catalogos/");
        for(String catalogoPath : catalogosPath){
            URI uri = context.getResource(catalogoPath).toURI();
            File arquivo = new File(uri.getPath());
            tempCat = (Catalogo) jaxbUnmarshaller.unmarshal(arquivo);
            catalogos.add(tempCat);
        }

Than tomcat is throwing me an error, it says that the file /localhost/cvrest/WEB-INF/catalogos/myfile.xml Doesnt exist but it exist . I'm trying to unmarshall it but it keeps me throwing no file found. I might be missunderstanding the return from getresourcepaths, it gives me an url, am i converting it right?

You can use context.getRealPath("/") inside your servlet to get the base path of your project. To get the list of files just append your file path relative to your project path, for example, context.getRealPath("/") + "/WEB-INF/classes/folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM