开发者

grails file uploads management in wars

I have developed a grails app which has user file uploads (docs, etc..), they are stored in the relative folder "web-app/upload".

My question is that I do not know what is the best way to perform automatically war deployments and keep this folder. Because when I redeploy in Tomcat the whole app folder is deleted and all the files are deleted.

Additionaly I need a 开发者_JAVA技巧generic configuration fron set an external location from this Files

Have you found a solution for that?

P.D.: If I use System.properties['base.dir'] the result is null, and if I use a ApplicationHolder.application.mainContext.getResource() it return a temp path. :(


You should not be uploading files into your WAR structure. You should upload them to some external location.


I was able to solve partial as follow

    //for development environment
    def root = System.properties['base.dir']?.toString()
    if(!root){
        //for production environment in war deplements
        def tmpRoot = ApplicationHolder.application.mainContext.getResource('WEB-INF').getFile().toString()
        root = tmpRoot.substring(0, tmpRoot.indexOf(File.separator + 'temp' + File.separator))
    }
    if(!root){
        throw new Exception('Not found a valid path')
    }
    return root + File.separator

I hope it can be useful to others

Regards, Yecid Pacífico


This code obtains the parent folder where the application is located:

String path = servletContext.getRealPath("/"); 
String parentStr = new File(path).getParentFile().getParent(); 

I mean, if the web application were located in D:\somefolder\myWeb

path would be D:\somefolder\myWeb\web-app

parentStr would be D:\somefolder

So you could save the files in D:\somefolder\files-outside-myWeb-context

Is it what you are looking for?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜