java web 获取根目录_javaweb中获取服务器端跟目录方法总结
先說(shuō)一下java工程下目錄的獲取的幾種方法,有用的看客可以看一下:
1.new File("relative path").getAbsolutePath();
2.System.getProperties.getProperty("user.dir");
然后進(jìn)入主題javaweb中
servlet中:
request.getSession().getServletContext().getRealPath();獲取跟目錄(例:C:\apache-tomcat-6.0.35\webapps\webTest)
Struts的aciontion中
ServletActionContext.getServletContext().getRealPath("");獲取跟目錄(例:C:\apache-tomcat-6.0.35\webapps\webTest)
知識(shí)點(diǎn)提示 aciontion中? ServletActionContext.getServletContext().getResourceAsStream("download/a.mp3")作為Inputstream返回。常用語(yǔ)struts文件下載。
servlet和action中
Thread.currentThread().getContextClassLoader().getResource("").getPath()獲取類(lèi)所在目錄(例:C:\apache-tomcat-6.0.35\webapps\CfmaAsmis\WEB-INF\classes)(友情提示:source folder(如每一個(gè)工程都有的src)和package不同,package在服務(wù)器中對(duì)應(yīng)著具體的文件夾,而sorcerer folder只是虛擬的,所以下圖中的applicationContext.xml 絕對(duì)路徑為:原函數(shù)+“applicationContext.xml”)
總結(jié)
以上是生活随笔為你收集整理的java web 获取根目录_javaweb中获取服务器端跟目录方法总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 中国电竞酒店突破2万家:西安郑州最多 玩
- 下一篇: java 同类调用方法_JAVA中同类中