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("/") + 你的路径

  • 相关阅读:
    自动化遍历-appcrawler
    Android adb实现原理
    win安装appium
    appium_android-常见的问题
    IOS项目目录结构
    svn命令的使用
    什么时候需要将析构函数定义为虚函数,如果不这么做,会存在什么问题?
    关于C++的疑问剖析
    Apple-Watch开发2 APPIcon设置
    Apple-Watch开发1
  • 原文地址:https://www.cnblogs.com/tele-share/p/9890070.html
Copyright © 2011-2022 走看看