如何在 grails 控制器中找到我的 servlet 的当前目录位置?(或 java servlet)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9799084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find the current directory location of my servlet in grails controller? (or java servlet)
提问by Atharva Johri
I am running running a webapp in tomcat 7, and I am trying to upload images into the webapps/{webappname}/images folder via a grails controller (similar to a java servlet). How do I get the path of this images folder in the servlet so I can store the uploaded files there?
我正在 tomcat 7 中运行一个 webapp,我试图通过 grails 控制器(类似于 java servlet)将图像上传到 webapps/{webappname}/images 文件夹。如何在 servlet 中获取此图像文件夹的路径,以便将上传的文件存储在那里?
采纳答案by Vijay Agrawal
you can use:
servletContext().getRealPath("/");
to get the physical path to your webapp base directory
您可以使用:
servletContext().getRealPath("/");
获取您的 webapp 基本目录的物理路径
However, be careful uploading anything into it as that area can usually get wiped out by re-deployment. It is generally advisable to upload data to a separate data-specific directory outside your webapp or to a persistent store (DB)
但是,将任何内容上传到其中时要小心,因为该区域通常会因重新部署而消失。通常建议将数据上传到 web 应用程序之外的单独的特定于数据的目录或持久存储 (DB)