ServletContext#getRealPath(File.separator) 在 tomcat 中给出 null

ServletContext#getRealPath(File.separator) giveing null in tomcat

我在 HttpServlet 中使用以下代码。

String pathToWeb = getServletContext().getRealPath(File.separator);

我的应用程序上面的代码运行良好,return 应用程序目录的真实路径。而我发送了另一个分支进行质量检查,但无法使用上述代码检测到真实路径。

总是 return null,在 windows 服务器上使用 java8,tomcat8。 请协助。

我已经解决过这样的问题,但无法找到解决方案。

使用/作为上下文根的路径:

getServletContext().getRealPath("/")

而不是 OS-specific 文件分隔符。