HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
獲取tomcat服務器上的靜態文件路徑(也可獲取本地絕對路徑):
String webAppFolderPath = request.getSession().getServletContext().getRealPath("");
得到的地址是服務器上war部署解壓后的webapp文件夾的路徑
但是這種獲取路徑方式如果是在用weblogic部署的服務器上,會得到一個"null"的返回值,這時候我們可以添加一個判斷
if (webAppFolderPath == null) {
//獲取到weblogic服務器上war解壓后的WEB-INF文件夾的路徑
webAppFolderPath = this.getClass().getClassLoader().getResource("/").getPath();
//要獲取webapp文件夾的路徑,簡單處理一下就可以了
webAppFolderPath = webAppFolderPath .substring(0,cacheFilePath.indexOf("WEB-INF"));
System.out.print(webAppFolderPath );
}