JavaWeb項目里面的路徑獲取方法總結


僅為資源搬運,個人還未充分理解...

request.getRealPath不推薦使用request.getRealPath("") 這個方法已經不推薦使用了

 

request.getRealPath不推薦使用request.getRealPath("") 這個方法已經不推薦使用了,那代替它的是什么方法 Deprecated. As of Version 2.1 of the Java Servlet API, use ServletContext.getRealPath(java.lang.String) instead. request.getSession().getServletContext().getRealPath() 得到站點的絕對地址

 

如request.getSession().getServletContext().getRealPath(File.separator) 得到項目的絕對路徑如:

 

D:\ProgramFiles\apache-tomcat-6.0.33\webapps\wt4\

 

request.getSession().getServletContext().getRealPath(“/updload/video/”) 得到項目的絕對路徑

 

D:\ProgramFiles\apache-tomcat-6.0.33\webapps\wt4\updload\video\

 


 

獲取class文件路徑

servletcontext.getRealPath() 方法已經不建議使用了

原則:基本上就是盡量使用j2ee規范中的各層次classloader來獲取資源,而不是試圖去找文件的絕對路
方法:調用this.getClass().getClassLoader().getResource("/").getPath(); 獲取到classes目錄的全路徑

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM