Search code examples
javatomcatgrailsgroovyuploading

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


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?


Solution

  • 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)