解决方案 »

  1.   

    ROOT路径?楼主是要获取classpath的路径吗?
    使用 Thread.currentThread().getContextClassLoader().getResource("/").getPath();  
    参考 java代码中获取classpath路径 
      

  2.   


    我取得的值是:/E:/workspace/HotelCloud/WebRoot/WEB-INF/classes/  , 而不是我想要的D:\apache-tomcat-7.0.54\webapps\ROOT 的路径
      

  3.   

    Thread.currentThread().getContextClassLoader().getResource("/").getPath();   取得就是你要的路径,你试试看。楼主试过 Thread.currentThread().getContextClassLoader().getResource("/").getPath();   这个代码了吗?取得是哪个路径呢?
      

  4.   


    我就是用你的代码获取的路径,结果是:/E:/workspace/HotelCloud/WebRoot/WEB-INF/classes/  ,我建的是tomcat工程,不是web工程,没能取到D:\apache-tomcat-7.0.54\webapps\ROOT 的路径
      

  5.   

    等等,你的代码在哪里运行呢?D:\apache-tomcat-7.0.54\webapps\ROOT 中?
    你建的是tomcat工程?不大理解,楼主详细说一下。
    那你试试java本身的 System.getProperty("user.dir") 取到的路径
      

  6.   

    等等,你的代码在哪里运行呢?D:\apache-tomcat-7.0.54\webapps\ROOT 中?
    你建的是tomcat工程?不大理解,楼主详细说一下。
    那你试试java本身的 System.getProperty("user.dir") 取到的路径我的代码是运行在我的工作空间,就是D盘的workspace,因为是tomcat工程,所以不会在tomcat安装目录下的webapp目录下存在这个工程,而是直接运行在我的工作空间,这就导致了我在servlet里通过request.getServletContext().getrealpath()取得的路径是E:/workspace/HotelCloud/WebRoot,而不是tomcat 安装目录的webroot路径
      

  7.   

    String filepath = this.getServletContext().getRealPath("");