【问题标题】:Getting a Web Accessible Directory in Spring-MVC/Java在 Spring-MVC/Java 中获取 Web 可访问目录
【发布时间】:2010-04-20 17:27:00
【问题描述】:

简单的问题,我需要将站点地图写入我的网络根目录,以便可以这样访问:

http://mysite.com/sitemap.xml

我希望代码看起来像:

File f = new File( someMethodToGetWebRootDirectory() );

但我不知道那个方法应该是什么

【问题讨论】:

  • 你的WEB-INF目录的父目录是根目录。 new File("sitemap.xml") 不会让你走上正确的道路吗?
  • 不,这是我尝试的第一件事,它把它放在我的应用程序服务器的某个配置文件夹中,这是另一件事,这需要跨网络服务器保持一致

标签: java jakarta-ee spring-mvc


【解决方案1】:

将类似于:

File classesDir = new File(YourServlet.class.getProtectionDomain()
    .getCodeSource().getLocation().getPath().replaceAll('/', File.separator));
File rootDir = classesDir.getParentFile().getParentFile();

工作?

【讨论】:

  • 可能没有适用于应用服务器的解决方案。可以想象一个从不将战争文件扩展到磁盘上的服务器。
猜你喜欢
  • 2018-08-04
  • 2011-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 2019-09-13
  • 2019-04-21
  • 1970-01-01
相关资源
最近更新 更多