views:

927

answers:

4

I have a simple grails file upload app.

I am using transeferTo to save the file to the file system.

To get the base path in my controller I am using

def basePath = System.properties['base.dir'] // HERE IS HOW I GET IT

        println "Getting new file"
        println "copying file to "+basePath+"/files"
        def f = request.getFile('file')
        def okcontents = ['application/zip','application/x-zip-compressed']
        if (! okcontents.contains(f.getContentType())) {
         flash.message = "File must be of a valid zip archive"
         render(view:'create', model:[zone:create])
         return;
        }
              if(!f.empty) {
                  f.transferTo( new File(basePath+"/files/"+zoneInstance.title+".zip") )
              }
              else 
              {
                  flash.message = 'file cannot be empty'
                      redirect(action:'upload')
              }
        println "Done getting new file"

For some reason this is always null when deployed to my WAS 6.1 server.

Why does it work when running dev but not in prod on the WAS server? Should I be accessing this information in a different way?

A: 

Grails, when it's run in dev mode, provides a whole host of environment properties to its Gant scripts and the app in turn, including basedir.

Take a look at the grails.bat or grails.sh script and you will find these lines:

Unix: -Dbase.dir="." \
Windows: set JAVA_OPTS=%JAVA_OPTS% -Dbase.dir="."

When these scripts start your environment in dev mode you get these thrown in for free.

When you take the WAR and deploy you no longer use these scripts and therefore you need to solve the problem another way; you can either

  1. Specify the property yourself to the startup script for the app server, eg: -Dbase.dir=./some/dir .. however
  2. ... it usually makes more sense to use the Grails Config object which allows for per-environment properties
j pimmel
+3  A: 

Thanks j,

I found the best dynamic solution possible. As a rule I never like to code absolute paths into any piece of software. Property file or no.

So here is how it is done:

def basePath = grailsAttributes.getApplicationContext().getResource("/files/").getFile().toString()

grailsAttributes is available in any controller.

getResource(some relative dir) will look for anything inside of the web-app folder.

So for example in my dev system it will toString out to "C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\ with the relative dir concated to the end

like so in my example above C:\WORKSPACEFOLDER\PROJECTFOLDER\web-app\files

I tried it in WAS 6.1 and it worked in the container no problems. You have to toString it or it will try to return the object.

mugafuga

mugafuga
A: 

There's a definitive way...

grailsApplication.parentContext.getResource("dir/or/file").file.toString()

Out of controllers (ex. bootstrap)? Just inject..

def grailsApplication

Best regards!

Wanderson Santos
A: 

How to get a .zip file from a GET request and save it to the local file-system?

Maurizio