I don't do Grails, but since it runs on top of the Servlet API, you may find this answer (a steering in the right direction) useful as well.
First, get a handle of the ServletContext
. Normally you would use the GenericServlet
-inherited getServletContext()
method for this. Then make use of the ServletContext#getRealPath()
method to convert a relative web path to an absolute local disk file system path (because that's the only which java.io.File
reliably understands).
String absolutePath = getServletContext().getRealPath("albums/1");
File file = new File (absolutePath);
// ...
If you use relative paths in java.io.File
stuff, then it will become relative to the current working directory which depends on the way how you startup the server and which indeed may be Tomcat/bin
as you experienced yourself.
That said, there's another major problem with this approach: if you create folders in an exploded webapp, they will get lost whenever you redeploy the webapp or even restart the server! Rather create folders outside the webapp's context, in a fixed path somewhere else at the disk file system. Or if you want better portability (but poorer metadata information), then consider storing it in a database instead.