servletContext.getRealPath("") returns 不同的结果 tomcat7/tomcat8 错误?
servletContext.getRealPath("") returns different results tomcat7/tomcat8 bug?
有人可以解释为什么 servletContext.getRealPath("")
returns 使用不同版本的 tomcat 会产生不同的结果吗?
- Tomcat 7: /this/is/my/path
- Tomcat 8:/this/is/my/path/(尾部斜杠)
这是一个错误,或者有人对此行为有很好的解释(和解决方法?):-)
是的,您是对的,在 Tomcat 8 中,getRealPath("") 应 return 您的路径末尾带有文件分隔符。文档如下: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
有人可以解释为什么 servletContext.getRealPath("")
returns 使用不同版本的 tomcat 会产生不同的结果吗?
- Tomcat 7: /this/is/my/path
- Tomcat 8:/this/is/my/path/(尾部斜杠)
这是一个错误,或者有人对此行为有很好的解释(和解决方法?):-)
是的,您是对的,在 Tomcat 8 中,getRealPath("") 应 return 您的路径末尾带有文件分隔符。文档如下: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