JavaEE路径陷阱之getRealPath
来源:互联网 发布:网站排名优化北京华网 编辑:程序博客网 时间:2024/04/29 19:15
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方法。
- JavaEE路径陷阱之getRealPath
- JavaEE路径陷阱之getRealPath
- JavaEE路径陷阱之getRealPath
- JavaEE路径陷阱之getRealPath
- JavaEE程序有一大路径陷阱,那就是ServletContext的 getRealPath方法
- servlet之getRealPath获取路径问题。
- getRealPath
- getRealPath
- getRealPath
- JavaEE 保存文件获取绝对路径getResource("")和servletContext.getRealPath("/")
- weblogic war 路径 getServletContext().getRealPath() null 解决办法
- servlet中getServletContext().getRealPath("/");的路径
- JavaEE路径配置
- JavaEE 获取路径
- war包发布找不见路径--weblogic --------getResource("/")与getRealPath("/")
- ServletActionContext.getServletContext().getRealPath("/upload");myeclipseyeclipse获取路径不一样的原因
- request.getRealPath("/")在Linux下获取路径的问题
- request.getContextPath()、request.getServletPath()、request.getRequestURI()、request.getRealPath("/")路径
- 遭遇Worm.Win32.Viking,Worm.Win32f.ysv,Trojan.PSW.Win32.OnlineGames等
- java时间操作函数汇总
- 名词解释 J2EE 全面简介
- Java路径问题最终解决方案—可定位所有资源的相对路径寻址
- Apche日志系列(1):访问日志
- JavaEE路径陷阱之getRealPath
- Java路径问题最终解决方案使用演示
- 无题……
- 如何编写可调试自诊断代码?
- 初来乍到!
- 以前的程序安装创建了挂起的文件操作。运行安装程序之前,必须重新启动计算机
- 使用 jQuery 简化 Ajax 开发
- 怀念农大时家的感觉!
- SmartNX Mobile操作系统与NX200智能手机平台正式跃上国际舞台