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

别来无恙 提交于 2020-01-06 14:59:01

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/17976322/how-to-configure-grails-to-avoid-deletion-of-uploaded-images-when-redeploy-in-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!