【问题标题】:How to get file system path of the context root of any application如何获取任何应用程序的上下文根的文件系统路径
【发布时间】:2011-11-19 14:38:29
【问题描述】:

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

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

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

请告诉我,我想上传一张图片放在我的web-application root/images/images.gif

【问题讨论】:

  • 您可以粘贴用于创建文件夹的代码吗?
  • 我用于存储图像的以下代码 ...String imagePath= request.getContextPath()+ System.getProperty("file.separator") + p_form.getCategorySelect() + System.getProperty(" file.separator") + p_form.getSubCatSelect() + System.getProperty("file.separator") + order_id +System.getProperty("file.separator") ;在此之后,我将使用此字符串创建文件并保存。

标签: java jsp jakarta-ee tomcat


【解决方案1】:

你把事情搞混了。 HttpServletRequest.getContextPath() 返回您的 Web 应用程序根路径。在您的示例中,这是 /streetshop,因此您的 URL 可能类似于 www.myapp.com/streetshop。如果要访问内部文件系统路径,必须使用request.getServletContext().getRealPath("/")ServletContext 获取。这应该会返回您的 WAR 文件的 WebContent 文件夹的位置。

请记住,如果您在运行时修改此路径的内容,您将在重新部署应用程序时丢失所有内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多