如何获取任何应用程序的上下文根的文件系统路径 [英] How to get file system path of the context root of any application

查看:58
本文介绍了如何获取任何应用程序的上下文根的文件系统路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发web应用程序。我在我的jsp request.getContextPath()上调用,但奇怪的是我得到了地址 / streetshop

I am working on web application.I invoke on my jsp request.getContextPath(), but strangely I got address /streetshop.

然后我将一些路径附加为 request.getContextPath()+abc并且创建文件夹。

Then I am appending some path as request.getContextPath() + "abc" and create folder.

然后在 D:// 中创建文件夹而不是我的webapplication文件夹。

Then its creating folder in D:// instead of my webapplication folder.

请告诉我,我想将图片上传到我的网络应用程序root / images / images.gif

Please, tell me, I want to upload an image in put it in my web-application root/images/images.gif.

推荐答案

你在这里混淆了。 HttpServletRequest.getContextPath()返回您的Web应用程序根路径。在您的示例中,这是 / streetshop ,因此您的网址可能类似于 www.myapp.com/streetshop 。如果要访问内部文件系统路径,则必须使用 ServletContext 获取它。 /api/javax/servlet/ServletContext.html#getRealPath%28java.lang.String%29rel =noreferrer> request.getServletContext()。getRealPath(/) 。这应该返回WAR文件的位置 WebContent 文件夹。

You mix things up here. HttpServletRequest.getContextPath() returns your web application root path. In your example this is /streetshop, so your URL may look similar to www.myapp.com/streetshop. If you want to access the internal file system path, you must obtain it from the ServletContext using request.getServletContext().getRealPath("/"). This should return the location of your WAR files' WebContent folder.

请记住,如果你修改内容在运行期间,这条路径在重新部署应用程序时会丢失所有内容。

Keep in mind that if you modify contents of this path during runtime, you're going to loose everything when redeploying your application.

这篇关于如何获取任何应用程序的上下文根的文件系统路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆