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

  • 相关阅读:
    C++中用Int转成bool时,只有0是false,其他都是true。这个和其他语言很不一样,注意不要掉坑里了。
    C# 获取动态验证码?
    Silverlight单元格事件
    LDAPHelper
    Perl内部保留变量(系统变量)
    WebSphere MQ基础命令
    老鼠, 老虎傻傻分不清楚之Double.NaN
    TextBlock or Label?
    如何阅读代码
    EDID
  • 原文地址:https://www.cnblogs.com/tele-share/p/9890070.html
Copyright © 2011-2022 走看看