java Servlet 真实路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17400077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet Real Path
提问by DD.
I am running a webapp under the directory blog. (e.g. www.example.com/blog).
我在目录 blog 下运行一个 webapp。(例如 www.example.com/blog)。
I would like to get the real filesystem path of a request.
我想获取请求的真实文件系统路径。
e.g. www.example.com/blog/test-file.html -> /usr/share/tomcat7/webapps/blog/test-file.html
例如 www.example.com/blog/test-file.html -> /usr/share/tomcat7/webapps/blog/test-file.html
I tried the following:
我尝试了以下方法:
public String realPath(HttpServletRequest request, ServletContext servletContext){
String requestURI = request.getRequestURI();
String realPath = servletContext.getRealPath(requestURI);
return realPath;
}
However this returns
然而这会返回
/usr/share/tomcat7/webapps/blog/blog/test-file.html
/usr/share/tomcat7/webapps/blog/blog/test-file.html
What is the correct way to do this?
这样做的正确方法是什么?
回答by informatik01
Short answer
简答
To get the result you want, use HttpServletRequest#getServletPath()
method as an argument to getRealPath()
method.
要获得您想要的结果,请使用HttpServletRequest#getServletPath()
method 作为 method 的参数getRealPath()
。
This is the closest to what you want to accomplish (read the note below).
这是最接近您想要完成的(阅读下面的注释)。
Explanation
解释
The reason you're getting such path (with double blog
) is that you're using the result returned by the getRequestURI()method.
您获得此类路径(使用 double blog
)的原因是您使用的是getRequestURI()方法返回的结果。
The getRequestURI()method returns the path starting with application context. In your case it will be:/blog/test-file.html
所述getRequestURI()方法返回开始与该路径应用上下文。在您的情况下,它将是:/blog/test-file.html
What happens then, the getRealPath()method appendsthe string returned by getRequestURI()method to the real/physical path to the folder, where you application resides on the file system, which in your case is:/usr/share/tomcat7/webapps/blog/
然后会发生什么,getRealPath()方法将getRequestURI()方法返回的字符串附加到文件夹的实际/物理路径,您的应用程序驻留在文件系统上,在您的情况下是:/usr/share/tomcat7/webapps/blog/
So the resulting path is:/usr/share/tomcat7/webapps/blog/blog/test-file.html
所以得到的路径是:/usr/share/tomcat7/webapps/blog/blog/test-file.html
That is the reason of your double blog
issue.
这就是你双重blog
问题的原因。
IMPORTANT NOTE
重要的提示
DISCLAIMER
Maybe the OP is already aware of the information written below, but it is written for the sake of completeness.
免责声明
也许 OP 已经知道下面写的信息,但为了完整性而写。
The real path you are trying to get does not meanyou are getting, well, the real path on your file system. The url-pattern
configured in web.xml(or if you're using Servlet 3.0+ in the related annotation) is actually a logical/virtual path, which may or may not relate to the actual, physical path on a file system, i.e. the patterns (paths) specified does not need to exist physically.
您尝试获取的真实路径并不意味着您正在获取文件系统上的真实路径。web.xml 中的url-pattern
配置(或者如果您在相关注释中使用 Servlet 3.0+)实际上是一个逻辑/虚拟路径,它可能与文件系统上的实际物理路径相关,也可能不相关,即模式(路径)指定的不需要物理存在。
Also quote from the ServletContext.getRealPath(String)documentation (emphasis mine):
还引用了ServletContext.getRealPath(String)文档(重点是我的):
Gets the real path corresponding to the given virtual path.
获取与给定虚拟路径对应的真实路径。