I have a simple grails file upload app.
I am using transferTo 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?
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:
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