Question

I would like to grab all the image file names in an image directory that is within a webapp directory (the one with the jsp files that my ActionBean uses).

My ActionBean is of course in the src directory which is alongside my webapp directory.

My ActionBean is able to refer to the jsps by using the path "/WEB-INF/jsps" I need to access "/images".

I tried doing:

File imageDir = new File("/images");
boolean real = imageDir.exists();

but I get false. I am really new with java.io. Can someone help me? How do I reach out to that directory?

Was it helpful?

Solution

Since you're invoking new File("/images") inside of a java application, java will think that you want a file at the root of the filesystem (an absolute path). Since you don't have an /images folder there, exists returns false.

Relative paths are tricky here too, because they depend on where tomcat is installed/run from. When I've done this before, I usually just spell out the whole path straight from the top level to the images folder ala "/home/myself/Public/tomcat/webapps/the-app/images/" or however your structure looks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top