在SpringMVC/Java中获取Web可访问目录

在SpringMVC/Java中获取Web可访问目录,java,jakarta-ee,spring-mvc,Java,Jakarta Ee,Spring Mvc,简单的问题是,我需要将站点地图写入我的web根目录,以便它可以访问: 我希望代码看起来像: File f = new File( someMethodToGetWebRootDirectory() ); 但我不知道这种方法应该是什么会有一些类似的东西: File classesDir = new File(YourServlet.class.getProtectionDomain() .getCodeSource().getLocation().getPath().replaceAl

简单的问题是,我需要将站点地图写入我的web根目录,以便它可以访问:

我希望代码看起来像:

File f = new File( someMethodToGetWebRootDirectory() );

但我不知道这种方法应该是什么

会有一些类似的东西:

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

工作?

WEB-INF目录的父目录是根目录。
new File(“sitemap.xml”)
是否为您找到了正确的路径?不,这是我尝试的第一件事,它将它放在我的应用程序服务器的某个配置文件夹中,这是另一件事,这需要在web服务器之间保持一致。可能没有一个解决方案可以跨应用程序服务器工作。你可以想象一个永远不会将war文件扩展到磁盘上的服务器。