僅為資源搬運,個人還未充分理解...
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目錄的全路徑