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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM