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 );
}