How can I find the current directory location of my servlet in grails controller? (or java servlet)

Atharva Johri picture Atharva Johri · Mar 21, 2012 · Viewed 10.6k times · Source

I am running running a webapp in tomcat 7, and I am trying to upload images into the webapps/{webappname}/images folder via a grails controller (similar to a java servlet). How do I get the path of this images folder in the servlet so I can store the uploaded files there?

Answer

Vijay Agrawal picture Vijay Agrawal · Mar 21, 2012

you can use: servletContext().getRealPath("/"); to get the physical path to your webapp base directory

However, be careful uploading anything into it as that area can usually get wiped out by re-deployment. It is generally advisable to upload data to a separate data-specific directory outside your webapp or to a persistent store (DB)