zoukankan      html  css  js  c++  java
  • request.getSession().getServletContext().getRealPath()的一些坑

    今天是学校机房的服务器上对之前的一个网站升级时发现了一个bug,我自己的机器上用的tomcat8,机房上是tomcat7,结果一运行就开始报找不到文件,最后发现是文件分隔符的问题

    原来在代码中涉及到路径的写法是request.getSession().getServletContext().getRealPath("/WEB-INF/"),总是File Not Found.于是修改为request.getSession().getServletContext().getRealPath("/") +"WEB-INF"+"/",重新编译运行后正常,很奇葩的问题下午抽个时间又对request.getSession().getServletContext().getRealPath()进行了一番测试,发现即使使用File.separator也没用,注意第5行结果

    1 System.out.println(request.getSession().getServletContext().getRealPath(""));   G:apache-tomcat-8.5.24webappsservlet
    2 System.out.println(request.getSession().getServletContext().getRealPath("/"));  G:apache-tomcat-8.5.24webappsservlet
    3 System.out.println(request.getSession().getServletContext().getRealPath(File.separator)); G:apache-tomcat-8.5.24webappsservlet
    4 System.out.println(request.getSession().getServletContext().getRealPath(File.separator+ "WEB-INF")); G:apache-tomcat-8.5.24webappsservletWEB-INF
    5 System.out.println(request.getSession().getServletContext().getRealPath(File.separator + "WEB-INF" + File.separator)); G:apache-tomcat-8.5.24webappsservletWEB-INF
    6 System.out.println(request.getSession().getServletContext().getRealPath("/WEB-INF/"));G:apache-tomcat-8.5.24webappsservletWEB-INF

    推荐写法request.getSession().getServletContext().getRealPath("/") + 你的路径

  • 相关阅读:
    SHELL脚本自动备份Linux系统
    Linux Shell脚本之自动修改IP
    oracle redo日志维护
    Linux运维工程师面试
    angular 的杂碎报错小知识
    angular.run 妙用
    vue的生命周期
    angular +H5 上传图片 与预览图片
    跨域问题解决方案之chrome插件
    js递归
  • 原文地址:https://www.cnblogs.com/tele-share/p/9890070.html
Copyright © 2011-2022 走看看