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