一、获得都是当前运行文件在服务器上的绝对路径
在servlet里用:
this.getServletContext().getRealPath();

在struts用:
this.getServlet().getServletContext().getRealPath();

在Action里用:
ServletActionContext.getRequest().getRealPath();

以上三个获得都是当前运行文件在服务器上的绝对路径
其实:
request.getRealPath("url"); // 虚拟目录映射为实际目录
request.getRealPath() 这个方法已经不推荐使用了,代替方法是:
request.getSession().getServletContext().getRealPath();

二、request获取各种路径总结
request.getRealPath("url"); // 虚拟目录映射为实际目录
request.getRealPath("./");    // 网页所在的目录
request.getRealPath("../"); // 网页所在目录的上一层目录
request.getContextPath();    // 应用的web目录的名称

如http://localhost:7001/bookStore/
/bookStore/ => [contextPath] (request.getContextPath())

获取Web项目的全路径
String strDirPath =request.getSession().getServletContext().getRealPath("/");

以工程名为news为例: http://localhost:8080/news/main/list.jsp

(1) 得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/news/main/list.jsp

(2) 得到工程名:request.getContextPath()
结果:/news

(3) 得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /main/list.jsp 

(4) 得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/news/main/list.jsp

(5) 得到相对地址:
request.getRequestURI()
结果:/news/main/list.jsp  

(6) 得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
结果:D:\resin\webapps\news\main\list.jsp

(7) 得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\news

三、在类中取得路径

(1) 类的绝对路径:Class.class.getClass().getResource("/").getPath()
结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/

例如:

String path=this.getClass().getResource("/").getPath();//得到d:/tomcat/webapps/工程名WEB-INF/classes/路径
path=path.substring(, path.indexOf("WEB-INF/classes"));//从路径字符串中取出工程路径

(2) 得到工程的路径:System.getProperty("user.dir")

结果:D:\TEST

四、在Servlet中取得路径
(1) 得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\news

import java.io.File;
public class Test {
  public static void main(String[] args) throws Exception {
     System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));
    System.out.println(Test.class.getClassLoader().getResource(""));
    System.out.println(ClassLoader.getSystemResource(""));
    System.out.println(Test.class.getResource(""));
    System.out.println(Test.class.getResource("/")); //Class文件所在路径
    System.out.println(new File("/").getAbsolutePath());
    System.out.println(System.getProperty("user.dir"));
  }
}

五、系统路径说明: 

System.getProperty("user.dir")

如果是.bat或者.sh文件的话,那么该语句输出的时候.bat或者.sh所在的文件路径;

如果是weblogic服务器且由类输出的话,该语句输出的路径是./startWebLogic.sh文件所在的路径

        String osName = System.getProperties().getProperty("os.name");
if(osName.equals("Linux")){
System.setProperty("CPATH",System.getProperty("user.dir")+"/cssconfig.properties");
}else{
System.setProperty("CPATH",System.getProperty("user.dir")+"\\cssconfig.properties");
}

根据操作系统设定不同路径。

最新文章

  1. JAVA第五次作业
  2. ASP.NET Web API 2.1支持Binary JSON(Bson)
  3. GacUI学习(一)
  4. SoapUI中如何传递cookie
  5. 移动设备优先viewport
  6. OAF_MDS系列2_OAF页面的通过MDS多语言开发国际化(案例)
  7. PERL DBI 自动重连问题
  8. json的遍历
  9. LeetCode OJ 152. Maximum Product Subarray
  10. Activiti工作流(二)之常用操作
  11. Android码农如何一个星期转为iOS码农(不忽悠)
  12. VMware Workstation 12 Pro 之安装林耐斯-Elementaryos-系统
  13. thymeleaf循环
  14. react组建生命周期
  15. SQL的decode()函数
  16. hdu-4180-exgcd
  17. JavaScript:ECMAScript 引用类型
  18. SpringBoot-基于Maven工程使用SpringBoot
  19. 单表扫描,MySQL索引选择不正确 并 详细解析OPTIMIZER_TRACE格式
  20. JS探秘——那些你理解存在偏差的问题

热门文章

  1. $《第一行代码:Android》读书笔记——第8章 通知和手机多媒体
  2. 关于pycharm中的requirements.txt文件
  3. 主攻ASP.NET.4.5 MVC4.0之重生:图书推荐
  4. axios拦截器/http
  5. CCNA 课程 三
  6. SpringBoot2.0之整合Kafka
  7. RealThinClient SDK 学习笔记(1)
  8. mongodb的使用(入门)
  9. java中@Qualifier("string")是什么用法
  10. SPOJ375 QTREE - Query on a tree