java代码访问远程服务器静态文件的方法

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

posted on 2019-10-30 16:34  jie丶mei  阅读(1738)  评论(0)    收藏  举报

导航