Search code examples
tomcat6

How to list files under a tomcat 6 webapp


I have a spring boot project that is deployed on Tomcat 6. I have a list of files under project's resource directory and I am trying to list all files under that directory. I am able to get specific file if I specify classpath:abc.txt in the getResource() method. However I am wondering if there is a way to just list all files with a wild card or something.


Solution

  • You cannot do like this MyClass.class.getResource('*.txt'). The alternative to this is :

    ServletContext      sc                      =       request.getSession().getServletContext();
        String          strPath                 =       sc.getRealPath("/");
    File[]          allFiles          =       new File(strpath).listFiles(new FilenameFilter()    {
        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".txt");
        }
    });
    

    Refer this :Using File.listFiles with FileNameExtensionFilter