对于java web各种路径的获取一直有点混乱,找个时间用代码实际测试了一下,做一下记录~
代码:
private void printPath(HttpServletRequest request){
printStr("request.getScheme()", request.getScheme());
printStr("request.getServerName()", request.getServerName());
printStr("request.getServerPort()", request.getServerPort());
printStr("request.getContextPath()", request.getContextPath());
printStr("request.getServletPath()", request.getServletPath());
printStr("request.getRequestURI()", request.getRequestURI());
printStr("request.getPathInfo()", request.getPathInfo());
printStr("request.getLocalPort()", request.getLocalPort());
printStr("request.getLocalAddr()", request.getLocalAddr());
printStr("request.getLocalName()", request.getLocalName());
System.out.println("=========session========");
ServletContext servletContext = request.getSession().getServletContext();
printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("/"));
printStr("servletContext.getContextPath()", servletContext.getContextPath());
printStr("servletContext.getServerInfo()", servletContext.getServerInfo());
}
private void printStr(String name, Object value){
System.out.println(name + ":" + value);
}
输出:
request.getScheme():http
request.getServerName():localhost
request.getServerPort():8080
request.getContextPath():/myweb
request.getServletPath():/test
request.getRequestURI():/myweb/test
request.getPathInfo():null
request.getLocalPort():8080
request.getLocalAddr():0:0:0:0:0:0:0:1
request.getLocalName():0:0:0:0:0:0:0:1
=========session========
servletContext.getRealPath('\'):D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb\
servletContext.getContextPath():/myweb
servletContext.getServerInfo():Apache Tomcat/7.0.57
-------------------------------------------------万恶的分割线-------------------------------------------------
此处注意一下这个方法:获取绝对路径
servletContext.getRealPath("/")
在web后台,如果想创建一个file对象,需使用到绝对路径。使用时的传值与返回值还是以示例说明:
printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("\\"));
输出:D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb\
printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("/"))
输出:D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb\
printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("key.txt"));
输出:D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb\key.txt
//一下几种传值输出一样的
printStr("servletContext.getRealPath('\\')", servletContext.getRealPath("//"));
printStr("servletContext.getRealPath('\\')", servletContext.getRealPath(""));
输出:D:\Develop\tomcat\apache-tomcat-7.0.57_jdk8\webapps\myweb
在来看一下getRealPath的源码:
@Override
public String getRealPath(String path) {
Resource resource = this.resourceLoader.getResource(getResourceLocation(path));
try {
return resource.getFile().getAbsolutePath();
}
catch (IOException ex) {
logger.warn("Couldn't determine real path of resource " + resource, ex);
return null;
}
}
getResourceLocation(path)的源码:
/**
* Build a full resource location for the given path, prepending the resource
* base path of this {@code MockServletContext}.
* @param path the path as specified
* @return the full resource path
*/
protected String getResourceLocation(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
return this.resourceBasePath + path;
}
到此就明白了,要是获取web根目录的绝对路径,传个空字符串(“”)就ok了,要是根目录下的其他路径,直接跟路径就好咯~