绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!应该使用ServletContext的getResource()方法,直接使用相对于Web应用根目录的相对路径来获取资源。

ServletContext接口中定位资源的方法

getResource

java.net.URL getResource(java.lang.String path)

throws java.net.MalformedURLException

Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.

This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.

The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.

This method returns null if no resource is mapped to the pathname.

Some containers may allow writing to the URL returned by this method using the methods of the URL class.

The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.

This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.

Parameters:

path - a String specifying the path to the resource

Returns:

the resource located at the named path, or null if there is no resource at that path

Throws:

java.net.MalformedURLException - if the pathname is not given in the correct form

getResourceAsStream

java.io.InputStream getResourceAsStream(java.lang.String path)

Returns the resource located at the named path as an InputStream object.

The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource. This method returns null if no resource exists at the specified path.

Meta-information such as content length and content type that is available via getResource method is lost when using this method.

The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.

This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader.

Parameters:

path - a String specifying the path to the resource

Returns:

the InputStream returned to the servlet, or null if no resource exists at the specified path

getRealPath

java.lang.String getRealPath(java.lang.String path)

Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..

The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).

Parameters:

path - a String specifying a virtual path

Returns:

a String specifying the real path, or null if the translation cannot be performed

说明

可以看到,ServletContext接口中的getResource()等方法,可以找到任何从应用程序的根目录开始的资源。包括在.war包这样的压缩文件中。参数必须以/开头。

而我们常用的getRealPath(“/”)方法,在.war包发布时,就会失效。会返回null。

因此,我们应该避免使用getRealPath(“/”)这样的方法来获取应用程序的绝对路径。

如果你不想使用之前写过的助手类ClassLoaderUtil 的public static URL getExtendResource(String relativePath)方法,那么你应该使用ServletContext接口的

java.net.URL getResource(java.lang.String path)

throws java.net.MalformedURLException

方法,URL对象可以方便的转为URI,和String对象。

尽管没有ServletContext的源码,但是我可以猜想到getResource等方法一定在底层使用了ClassLoader的getResource方法。

java getrealpath_JavaEE路径陷阱之getRealPath相关推荐

  1. JavaEE路径陷阱之getRealPath

    转自:http://blog.csdn.net/shendl/article/details/1427637   JavaEE路径陷阱之getRealPath 本文是<Java路径问题最终解决方 ...

  2. java getrealpath_从request获取各种路径总结 request.getRealPath(url)

    equest.getRealPath() 这个方法已经不推荐使用了,代替方法是: request.getSession().getServletContext().getRealPath() 从Req ...

  3. Java相对路径/绝对路径总结(转)

    1.基本概念的理解 绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如: C:xyz est.txt 代表了test.txt文件的绝对路径.http://www. ...

  4. java 访问路径_java获取项目访问路径的相关方法

    java获取项目访问路径的相关方法 刘振兴 代码分享 2016年02月11日 6292 暂无评论 在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中 ...

  5. java web 路径 .html,java web 路径(java web 路径).doc

    java web 路径(java web 路径) java web 路径(java web 路径) The path used in Java, divided into two types: abs ...

  6. 关于Java文件路径问题

    转发:http://lib.csdn.net/article/javase/2046 关于Java文件路径问题 作者:soleghost 1.如何获得当前文件路径 常用: 字符串类型:System.g ...

  7. java 文件路径怎么写_java本地文件路径怎么写

    1. java里文件路径怎么写 File file = new File("D:\\123.txt"); 你这种不用绝对路径是不行的, 只有一个方法,在web工程启动servlet ...

  8. java文件路径转义_java文件路径中“\”和“/”的区别

    java文件路径中"\"和"/"的区别 发布时间:2020-06-25 17:51:22 来源:亿速云 阅读:189 作者:Leah 本篇文章为大家展示了jav ...

  9. java 获取包路径_java获取java文件路径的四种方法

    java获取java文件路径的四种方法 发布时间:2020-04-17 11:03:45 来源:亿速云 阅读:750 作者:小新 今天小编给大家分享的是java获取java文件路径的四种方法,很多人都 ...

最新文章

  1. 剑指offer:两个链表的第一个公共结点
  2. 剑指Offer——当当+搜狐+好未来笔试题+知识点总结
  3. 吴恩达机器学习006分类问题
  4. 使用.Net访问Office编程接口
  5. 分裂的奶牛群(洛谷P2907题题解,Java语言描述)
  6. 初识二进制2:指令系统
  7. MindManager思维导图中文版免费下载使用教程
  8. 图片点击后出现模态框效果的实现
  9. 处女座的期末复习-贪心
  10. 数据库SQL语言的使用
  11. 华为数通笔记-数通基本概念
  12. 安装VS2008 SP1
  13. vue上线后图片不显示_vue页面中图片不显示解决
  14. 美规Homekit插座
  15. 京东2018秋招编程题
  16. 旋转目标检测 校准的深度特征用于目标检测SSA
  17. 一个人被提拔,不仅仅是能力,而是信任
  18. 第四回 还君明珠双泪垂,恨不相逢未嫁时
  19. Day3_Pytorch入门——人脸标点绘图(简单)
  20. vba移动文件_VBA学习笔记8:工作表操作

热门文章

  1. Matlab xlim ylim zlim函数
  2. C++插入中文到mysql乱码
  3. oracle共享服务器模式的图,Oracle 11g笔记——专有服务器、共享服务器模式
  4. php项目私有化部署保护代码,ThinkPHP项目安全配置解决方案
  5. java获取默认用户目录_Java获取当前路径
  6. BurpSuite插件 -- Struts2-RCE
  7. mysql 5.7 启动脚本_MySQL数据库 5.7 启动脚本
  8. 我理解中的“大前端”/“大无线”
  9. margin折叠-从子元素margin-top影响父元素引出的问题
  10. HTMLCSS 问题