简体   繁体   中英

How to configure Grails to avoid deletion of uploaded images when redeploy in a Tomcat server

I have been reading about the best place to save images uploaded by users . In my case, I think it is better to store these images in the filesystem. I want to store them in the same server and keep it easy to mantain by other administrators.

Is it possible to set the upload folder inside the application context and configure Grails in order to not delete that folder when redeploying a new war?

If not, I wonder if the next code (gotten here by @yecid) would work as I think it uploads the images inside the application context:

//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

In the code, I see that your are saving uploaded files into your WEB-INF folder. Although you can do that, when you restart server/ redeploy application, the content of your web-app folder will be replaced, hence the uploaded file will be removed as well. That's how Tomcat works.

I think the better way to do that is saving your uploaded file to a folder which is NOT inside of your web container (Tomcat) folder. By that way, it won't be deleted when you:

1) Redeploy your web application

2) Restart Tomcat for whatever reason

A practice my team used to do is that we create a custom folder in the home folder of the current user. By that way we don't have to worry much about the privilege to save files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM