System.out.println("---" + request.getServletPath()); // 获取servlet地址:/uploadDemo
System.out.println("---" + request.getRequestURI()); // 获取servlet地址:/uploadDemo
System.out.println("---" + request.getRequestURL()); // servlet全路径:http://localhost:8080/uploadDemo
// 项目在容器中的实际发布运行的根路径,D:/pwx/upload/target/upload/
// 可以具体到包名 mode D:/pwx/upload/target/upload/mode
System.out.println("---" + request.getSession().getServletContext().getRealPath("/"));
System.out.println("---" + this.getServletContext().getRealPath("mode"));
// 用户当前的工作目录,D:/pwx/util/apache-tomcat-8.0.53/bin
System.out.println("---" + System.getProperty("user.dir"));
// 编译后,执行时指定的class所在的目录,/D:/pwx/upload/target/upload/WEB-INF/classes/
System.out.println("---" + Upload.class.getResource("/").getPath());
System.out.println("---" + this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
System.out.println("---" + this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile());
// 获取类资源文件做流
System.out.println("---" + Upload.class.getClassLoader().getResourceAsStream("Test.json"));
Java类路径以及HttpServletRequest获取各种路径总结
于 2019-05-07 20:41:58 首次发布