Java Web類路徑,項目路徑的擷取問題__java

來源:互聯網
上載者:User
//類載入根路徑String classPath = this.getClass().getResource("/").getPath();//類載入根路徑URL xmlPath = this.getClass().getClassLoader().getResource("");//類所在工程根路徑String proClassPath = this.getClass().getResource("").getPath();//專案服務器指令檔路徑File directory = new File("");// 參數為空白String proRootPath = directory.getCanonicalPath();//專案服務器指令檔路徑String proPath = System.getProperty("user.dir");// 擷取所有的類路徑 包括jar包的路徑String allClassPath = System.getProperty("java.class.path");//項目部署的路徑String path = request.getSession().getServletContext().getRealPath("/");System.out.println("類載入根路徑:" + classPath);System.out.println("類載入根路徑:" + xmlPath);System.out.println("類所在工程路徑:" + proClassPath);System.out.println("專案服務器指令檔路徑:" + proRootPath);System.out.println("專案服務器指令檔路徑:" + proPath);System.out.println("項目部署的路徑:" + allClassPath );System.out.println("擷取所有的類路徑:" + path );

結果如下:

類載入根路徑:/D:/workspace/Training2016/04-PG/0401-Source/base2016/base_web/target/base_web-1.0-SNAPSHOT/WEB-INF/classes/類載入根路徑:file:/D:/workspace/Training2016/04-PG/0401-Source/base2016/base_web/target/base_web-1.0-SNAPSHOT/WEB-INF/classes/類所在工程路徑:/D:/workspace/Training2016/04-PG/0401-Source/base2016/base_web/target/base_web-1.0-SNAPSHOT/WEB-INF/classes/com/study/mvc/專案服務器指令檔路徑:D:\apache-tomcat-8.0.361\bin專案服務器指令檔路徑:D:\apache-tomcat-8.0.361\bin項目部署的路徑:D:\workspace\Training2016\04-PG\0401-Source\base2016\base_web\target\base_web-1.0-SNAPSHOT\擷取所有的類路徑:D:\apache-tomcat-8.0.361\bin\bootstrap.jar;D:\apache-tomcat-8.0.361\bin\tomcat-juli.jar

項目路徑如圖所示,target為部署的位置

注意此處可能request擷取不到,擷取request的方法有很多:

1.通過註解擷取(推薦):public class Hello {@Autowired  HttpServletRequest request; //這裡可以擷取到request}2.在web.xml中配置一個監聽:<listener>          <listener-class>  org.springframework.web.context.request.RequestContextListener          </listener-class>  </listener>  java代碼:HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  3.直接在參數中傳遞:public String hello(HttpServletRequest request)4.如果有Struts:HttpServletRequest request = ServletActionContext.getRequest(); 

有時request.getSession().getServletContext().getRealPath(“/”);擷取到的路徑不是tomcat的路徑

D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\....

這個的原因其實 D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\ 這個目錄就是eclipse的對 D:\Tomcat 7.0 目錄的一個複製,從而使 D:\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\ 也能夠具備原始伺服器的功能。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.