gpt4 book ai didi

tomcat - servletContext.getRealPath ("") 返回不同的结果 tomcat7/tomcat8 错误?

转载 作者:行者123 更新时间:2023-11-28 22:02:20 24 4
gpt4 key购买 nike

有人可以解释为什么 servletContext.getRealPath("") 在使用不同版本的 tomcat 时返回不同的结果吗?

  • Tomcat 7:/this/is/my/path
  • Tomcat 8:/this/is/my/path/(尾部斜线)

这是一个错误或有人对此行为有一个很好的解释(和解决方法?):-)

最佳答案

是的,您是对的,在 Tomcat 8 中,getRealPath("") 将向您返回末尾带有文件分隔符的路径。这是文档:Java doc - getRealPath

Gets the real path corresponding to the given virtual path.

For example, if path is equal to /index.html, this method will return the absolute file path on the server's filesystem to which a request of the formhttp://://index.html would be mapped, where  corresponds to the context path of this ServletContext.

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators.

Resources inside the /META-INF/resourcesdirectories of JAR files bundled in the application's/WEB-INF/lib directory must be considered only if the container has unpacked them from their containing JAR file, in which case the path to the unpacked location must be returned.

This method returns null if the servlet container is unable to translate the given virtual path to a real path.

Parameters:path - the virtual path to be translated to a real pathReturns:the real path, or null if the translation cannot be performed

关于tomcat - servletContext.getRealPath ("") 返回不同的结果 tomcat7/tomcat8 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com