JavaEE路徑陷阱之getRealPath

JavaEE路徑陷阱之getRealPath

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路徑問題最終解決方案—可定位所有資源的相對路徑尋址》中提出的助手類ClassLoaderUtilpublic static URL getExtendResource(String relativePath)方法,那麼你應該使用ServletContext接口的
java.net.URL getResource(java.lang.String path)
                         throws java.net.MalformedURLException
方法,URL對象可以方便的轉爲URI,和String對象。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章