Search code examples
javaspringpathrelative-pathportlet

Reading the content of a directory inside my project's webcontent folder


How to use a relative path to read the content of a directory kept inside the Web content folder of my PORTLET application (using spring framework). As I have found out the resources (XMLs, property files) can be read using resource loader or class.getResourceAsStream() but nothing works for accessing the folder.


Solution

  • Using the classloader you should be able to get hold of the folder.

    For example if you have a "templates" folder in you classes dir:

    try {
      URL u = getClass().getClassLoader().getResource("templates");
      File f = new File(u.getFile());
      File[] dir = f.listFiles();
      for(int i=0;i<dir.length;i++) {
        System.out.println(dir[i].getName());
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
    

    Perhaps your container is restricting access based on the relative path?