代碼以下:windows
// 當前用戶程序所在目錄 String userDir = System.getProperties().getProperty("user.dir");
在windows環境下取得的路徑是相似這樣的:tomcat
D:/workspace/sms
由於Linux和windows的分隔符不一樣,因此若是想獲取具體文件絕地路徑,須要這樣寫,例如獲取WebRoot下的sys_param.xls文件路徑:spa
// 分隔符 String fileSeperator = File.separator; String templateFilePath = userDir + fileSeperator + "WebRoot" + fileSeperator + "sys_param.xls";
在windows環境下取得的路徑是相似這樣的:code
D:/workspace/sms/WebRoot/sys_param.xls
更新:
以上方法只能在本地運行起來,可是在tomcat上並不能獲取到正確的路徑。
應該使用如下方式:get
// arg0是從上面方法傳下來的,類型是ServletContextEvent String templateFilePath = arg0.getServletContext().getRealPath("sys_param.xls");