今天是学校机房的服务器上对之前的一个网站升级时发现了一个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行结果

 System.out.println(request.getSession().getServletContext().getRealPath(""));   G:\apache-tomcat-8.5.24\webapps\servlet\
System.out.println(request.getSession().getServletContext().getRealPath("/")); G:\apache-tomcat-8.5.24\webapps\servlet\
System.out.println(request.getSession().getServletContext().getRealPath(File.separator)); G:\apache-tomcat-8.5.24\webapps\servlet
System.out.println(request.getSession().getServletContext().getRealPath(File.separator+ "WEB-INF")); G:\apache-tomcat-8.5.24\webapps\servlet\WEB-INF
System.out.println(request.getSession().getServletContext().getRealPath(File.separator + "WEB-INF" + File.separator)); G:\apache-tomcat-8.5.24\webapps\servlet\WEB-INF
System.out.println(request.getSession().getServletContext().getRealPath("/WEB-INF/"));G:\apache-tomcat-8.5.24\webapps\servlet\WEB-INF\

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

request.getSession().getServletContext().getRealPath()的一些坑的更多相关文章

  1. request.getSession().getServletContext().getRealPath("")获取工程目录 路径修改

    使用request.getSession().getServletContext().getRealPath("")获取工程目录. 设置server Locations在serve ...

  2. String filePath = request.getSession().getServletContext().getRealPath("/");这句话返回的路径是什么,解释下getRealPath("/")函数中的"/"表示什么意思

    request.getSession().getServletContext() 获取的是Servlet容器对象,相当于tomcat容器了.getRealPath("/") 获取实 ...

  3. 解决request.getSession().getServletContext().getRealPath("/")为null问题

    今天把程序部署到服务器,发现异常,FileNotFound异常,很快定位到getServletContext().getRealPath("/");返回空的问题.这个问题通常是传递 ...

  4. request.getSession().getServletContext().getRealPath("upload/" ); 获取不到 tomcat 服务器目录

    上传一个文件,找不到该文件的位置 设置上传的文件是在项目中的话 可以通过查找项目的路径锁定上传的文件路径 解决步骤: 可以通过jsp页面 打印获取项目的物理路径 控制台即可输出项目路径 这只是找了了上 ...

  5. spring boot 的request.getServletContext().getRealPath路径获取问题

    默认情况下springboot中request.getServletContext().getRealPath 返回的是一个临时文件夹的地址 通过查看源代码 位置在 org.springframewo ...

  6. 如何获得 request, "request.getSession(true).setAttribute("a",a);"与“request.setAttribute("a",a);”区别

    protected ServletContext getServletContext() { return ServletActionContext.getServletContext();} pro ...

  7. request.getSession(true/false)的区别

    javax.servlet.http.HttpServletRequest接口有两个方法:getSession(boolean)和getSession(). 具体什么区别,跟踪源码分析下,先摆出结论: ...

  8. this.getServletContext().getRealPath("WEB-INF");

    this.getServletContext().getRealPath("WEB-INF");

  9. idea编辑器HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath();方法无法使用

    HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath(); 前几天在使用idea的时候发现这个方 ...

随机推荐

  1. stm32单片机下载方式

    引用   编辑:什么鱼 引用地址:http://www.eeworld.com.cn/mcu/2015/1012/article_22873.html 第一种 ISP下载: 这里类似51. boot1 ...

  2. Transact-SQL语法速查手册

    第1章 Transact-SQL基础 1.1 标识符 一.常规标识符 1. 命名规则: l 第一个字母必须是Unicode2.0标准定义的字母.下划线.at符号(@)和数字符号(#): l 后续字符可 ...

  3. HDU 2587 - 很O_O的汉诺塔

    看题传送门 吐槽题目 叫什么很O_O的汉诺塔我还@.@呢. 本来是想过一段时间在来写题解的,不过有人找我要. 本来排名是第8的.然后搞了半天,弄到了第五.不过代码最短~ 截止目前就9个ID过,小小的成 ...

  4. FZU 2020 组合

    组合数求模要用逆元,用到了扩展的欧几里得算法. #include<cstdio> int mod; typedef long long LL; void gcd(LL a,LL b,LL ...

  5. fdopen:让文件描述符像文件一样使用

    FILE * fdopen(int fildes,const char * mode); fdopen与fopen类似,返回一个FILE *类型的值,不同的是此函数以文件描述符而非文件作为参数. 如果 ...

  6. VS无法访问IIS元数据库 您没有足够的特权访问计算机上的IIS网站

    进入windows\regedit.exe下的HKEY_CRRENT_USER\Software\Microsoft\Windows\CurrentVersion\Exploer\User Shell ...

  7. LDR: LdrpWalkImportDescriptor() failed to probe C:\WINDOWS\system32\opencv_core243d.dll for its manifest, ntstatus 0xc0150002

    LDR: LdrpWalkImportDescriptor() failed to probe C:\WINDOWS\system32\opencv_core243d.dll for its mani ...

  8. Yarn架构基本概况(一)

    1)引言 针对MRv1在扩展性.可靠性,资源利用率和多框架的支持上存在着明显的不足.进而诞生了下一代的MapReduce的计算框架MapReduce Version2,MRV1中有一个非常大的问题就是 ...

  9. Html表单中遇到的问题

    原文 https://www.jianshu.com/p/4466b8294007 大纲 1.表单提交的方式GET和POST的区别 2.js无法对input的file类型的值进行赋值 3.js获取in ...

  10. 【2006】求N!的精确值

    Time Limit: 3 second Memory Limit: 2 MB 对于阶乘函数,即使自变量较小,其函数值也会相当大.例如: 10!=3628800 25!=155112100433309 ...