6

I am attempting to run a Java web project under Tomcat 8 that traditionally runs under WebSphere. One servlet makes the following call:

xslFilePath = config.getServletContext().getRealPath(System.getProperty("file.separator") + "xsl");

config is an instance of ServletConfig.

The xsl is inside the project and deployed as C:\myproject\build\web\xsl. When the servlet attempts to reference a file located in xslFilePath, I get an exception which indicates that Tomcat is actually looking for the xsl file in C:\Program Files\Apache Software Foundation\Apache Tomcat 8.0.3\bin\null. Obviously this is the wrong location, and nothing is found.

Unfortunately I can't change the code because I don't have access to the source. So I would like to know if this is the expected behavior for Tomcat? Is there any Tomcat configuration that will let me ensure the path is referenced to the deployment directory and not to the Tomcat bin directory? Would choosing some other servlet container be preferable? Any advice would be appreciated.

4

1 回答 1

10

使用 getRealPath("/xsl")。

getRealPath() 的参数是“虚拟路径”,不幸的是,它是 Java 文档中使用的概念,但实际上并未在任何地方定义。它被假定为您的 Web 应用程序中资源的路径,并且在这种情况下,分隔符始终为“/”,无论平台如何。

于 2014-08-29T06:23:13.677 回答