转自:http://blog.csdn.net/shendl/article/details/1427637

  JavaEE路径陷阱之getRealPath
本文是《Java路径问题最终解决方案—可定位所有资源的相对路径寻址》一文的姐妹篇。请同时阅读该文。
JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!提供这个方法绝对是JavaEE API开发组的一大败笔。使用它,我们会万劫不复!
绝对不要使用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(“/”)这样的方法来获取应用程序的绝对路径。
如果你不想使用我在《Java路径问题最终解决方案—可定位所有资源的相对路径寻址》中提出的助手类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方法。

转载于:https://www.cnblogs.com/x_wukong/p/3719932.html

JavaEE路径陷阱之getRealPath相关推荐

  1. java getrealpath_JavaEE路径陷阱之getRealPath

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

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

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

  3. web工程部署路径与getServletConfig().getRealPath()

    我们都知道,通过ServletContext类可以获取工程路径(虚拟路径)以及工程部署在服务器硬盘上的绝对路径 那我们首先应该了解一个工程是怎样部署的,以idea为例 不知道大家有没有发现每当我们启动 ...

  4. Java路径问题最终解决方案使用演示

    Java路径问题最终解决方案使用演示 前言 在<Java路径问题最终解决方案-可定位所有资源的相对路径寻址>一文中,我给大家提供了一个助手类ClassLoaderUtil ,和它的publ ...

  5. JavaEE 获取路径全攻略

    本篇博客是 JavaWeb 应用服务器端在不同环境下获取文件路径的全面总结. 获取文件路径后主要应用的场景,读取 JavaWeb 自定义配置文件.在特定路径下生成各种类型的文件提供下载...... 想 ...

  6. 从request获取各种路径总结 转载:http://blog.csdn.net/piaoxuan1987/article/details/8541839 equest.getRealPath()

    https://www.cnblogs.com/hello-yao-ge/p/6606564.html 转载:http://blog.csdn.net/piaoxuan1987/article/det ...

  7. 从request获取各种路径request.getRealPath()

    1.此方法已过时,现在可替换的实现方法有 request.getSession().getServletContext().getRealPath("/") 在Servlet 里用 ...

  8. JAVA一个项目的路径为_java 得到项目路径

    一 相对路径的获得 说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目) String relativelyPath=System.getProper ...

  9. 获得虚拟服务器相对路径,在web应用中获取相对路径和绝对路径

    1.jsp中取得路径: 以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.getRequestURI() 结果:/TEST/test.jsp (2)得到工程名:reque ...

最新文章

  1. 【算法笔记】竞赛图(有向完全图)(相关题型总结)
  2. python实现多个gui互相协作或这多个软件同时服务一个业务的方式
  3. 选择排序法python详解-Python选择排序算法(三)
  4. Python easy_install
  5. java中Date与DateFormat的格式输出
  6. css 回到首页按钮,js+css实现回到顶部按钮(back to top)
  7. 数字图像处理 空间域锐化 MATLAB实验
  8. 三种动态创建元素区别
  9. 创建一个Django项目
  10. android高仿ios11系统,安卓仿ios11桌面全套文件
  11. 使用ARCGIS对shp数据添加投影坐标系
  12. javaweb从入门到精通教程,7天彻底搞定javaweb
  13. 微信小程序的Django后端极简部署
  14. NLP入门(七)中文预处理之繁简体转换及获取拼音
  15. ddl是什么意思网络语_跟随你大学的流行词语 DDL 你竟还不知道?
  16. 双十一,美团为何静悄悄?
  17. JavaScript实现的转盘抽奖html页面前端源码
  18. 5、特征选择(filter):方差分析(ANOVA)
  19. 算符优先分析法-java实现
  20. 如何将mp4视频制作成gif表情包

热门文章

  1. java args例子_Spring AOP中使用args表达式的方法示例
  2. python编程print语法_Python 第一节 安装及 print 输出语句—教案
  3. python的功能介绍_Python之int内部功能介绍
  4. html网页文件的主体标记,HTML的填空题:1.HTML文件中网页文件的主体标记是_________,标记页面标题的标记是_____________。如题 谢...
  5. 【哈渡谱】带你玩转Hadoop之《主机映射、镜像挂载与SSH免密篇》
  6. KALI LINUX文件名乱码,在kali linux之下 下载并解压的文件名呈现乱码 解决方案
  7. 回车的ascii码_ASCII码表
  8. python中使用html前端页面显示图像预测结果(改进)
  9. python实现socket编程,服务端开启多线程(和多个客户端聊天)
  10. 关于学习Python的一点学习总结(17->print打印参数及导入时重命名)