类 ResourceUtils


  • public abstract class ResourceUtils
    extends Object
    Utility methods for resolving resource locations to files in the file system. Mainly for internal use within the framework.

    Consider using Spring's Resource abstraction in the core package for handling all kinds of file resources in a uniform manner. ResourceLoader's getResource() method can resolve any location to a Resource object, which in turn allows one to obtain a java.io.File in the file system through its getFile() method.

    从以下版本开始:
    1.1.5
    作者:
    Juergen Hoeller
    另请参阅:
    Resource, ClassPathResource, FileSystemResource, UrlResource, ResourceLoader
    • 方法概要

      所有方法 静态方法 具体方法 
      修饰符和类型方法说明
      static URLextractArchiveURL​(URL jarUrl)
      Extract the URL for the outermost archive from the given jar/war URL (which may point to a resource in a jar file or to a jar file itself).
      static URLextractJarFileURL​(URL jarUrl)
      Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).
      static FilegetFile​(String resourceLocation)
      Resolve the given resource location to a java.io.File, i.e. to a file in the file system.
      static FilegetFile​(URI resourceUri)
      Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.
      static FilegetFile​(URI resourceUri, String description)
      Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.
      static FilegetFile​(URL resourceUrl)
      Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
      static FilegetFile​(URL resourceUrl, String description)
      Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
      static URLgetURL​(String resourceLocation)
      Resolve the given resource location to a java.net.URL.
      static booleanisFileURL​(URL url)
      Determine whether the given URL points to a resource in the file system, i.e. has protocol "file", "vfsfile" or "vfs".
      static booleanisJarFileURL​(URL url)
      Determine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.
      static booleanisJarURL​(URL url)
      Determine whether the given URL points to a resource in a jar file.
      static booleanisUrl​(String resourceLocation)
      Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.
      static URItoURI​(String location)
      Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.
      static URItoURI​(URL url)
      Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.
      static voiduseCachesIfNecessary​(URLConnection con)
      Set the "useCaches" flag on the given connection, preferring false but leaving the flag at true for JNLP based resources.
    • 方法详细资料

      • isUrl

        public static boolean isUrl​(String resourceLocation)
        Return whether the given resource location is a URL: either a special "classpath" pseudo URL or a standard URL.
        参数:
        resourceLocation - the location String to check
        返回:
        whether the location qualifies as a URL
        另请参阅:
        CLASSPATH_URL_PREFIX, URL
      • getURL

        public static URL getURL​(String resourceLocation)
                          throws FileNotFoundException
        Resolve the given resource location to a java.net.URL.

        Does not check whether the URL actually exists; simply returns the URL that the given location would correspond to.

        参数:
        resourceLocation - the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
        返回:
        a corresponding URL object
        抛出:
        FileNotFoundException - if the resource cannot be resolved to a URL
      • getFile

        public static File getFile​(String resourceLocation)
                            throws FileNotFoundException
        Resolve the given resource location to a java.io.File, i.e. to a file in the file system.

        Does not check whether the file actually exists; simply returns the File that the given location would correspond to.

        参数:
        resourceLocation - the resource location to resolve: either a "classpath:" pseudo URL, a "file:" URL, or a plain file path
        返回:
        a corresponding File object
        抛出:
        FileNotFoundException - if the resource cannot be resolved to a file in the file system
      • getFile

        public static File getFile​(URL resourceUrl)
                            throws FileNotFoundException
        Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
        参数:
        resourceUrl - the resource URL to resolve
        返回:
        a corresponding File object
        抛出:
        FileNotFoundException - if the URL cannot be resolved to a file in the file system
      • getFile

        public static File getFile​(URL resourceUrl,
                                   String description)
                            throws FileNotFoundException
        Resolve the given resource URL to a java.io.File, i.e. to a file in the file system.
        参数:
        resourceUrl - the resource URL to resolve
        description - a description of the original resource that the URL was created for (for example, a class path location)
        返回:
        a corresponding File object
        抛出:
        FileNotFoundException - if the URL cannot be resolved to a file in the file system
      • getFile

        public static File getFile​(URI resourceUri)
                            throws FileNotFoundException
        Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.
        参数:
        resourceUri - the resource URI to resolve
        返回:
        a corresponding File object
        抛出:
        FileNotFoundException - if the URL cannot be resolved to a file in the file system
        从以下版本开始:
        2.5
      • getFile

        public static File getFile​(URI resourceUri,
                                   String description)
                            throws FileNotFoundException
        Resolve the given resource URI to a java.io.File, i.e. to a file in the file system.
        参数:
        resourceUri - the resource URI to resolve
        description - a description of the original resource that the URI was created for (for example, a class path location)
        返回:
        a corresponding File object
        抛出:
        FileNotFoundException - if the URL cannot be resolved to a file in the file system
        从以下版本开始:
        2.5
      • isFileURL

        public static boolean isFileURL​(URL url)
        Determine whether the given URL points to a resource in the file system, i.e. has protocol "file", "vfsfile" or "vfs".
        参数:
        url - the URL to check
        返回:
        whether the URL has been identified as a file system URL
      • isJarURL

        public static boolean isJarURL​(URL url)
        Determine whether the given URL points to a resource in a jar file. i.e. has protocol "jar", "war, ""zip", "vfszip" or "wsjar".
        参数:
        url - the URL to check
        返回:
        whether the URL has been identified as a JAR URL
      • isJarFileURL

        public static boolean isJarFileURL​(URL url)
        Determine whether the given URL points to a jar file itself, that is, has protocol "file" and ends with the ".jar" extension.
        参数:
        url - the URL to check
        返回:
        whether the URL has been identified as a JAR file URL
        从以下版本开始:
        4.1
      • extractJarFileURL

        public static URL extractJarFileURL​(URL jarUrl)
                                     throws MalformedURLException
        Extract the URL for the actual jar file from the given URL (which may point to a resource in a jar file or to a jar file itself).
        参数:
        jarUrl - the original URL
        返回:
        the URL for the actual jar file
        抛出:
        MalformedURLException - if no valid jar file URL could be extracted
      • extractArchiveURL

        public static URL extractArchiveURL​(URL jarUrl)
                                     throws MalformedURLException
        Extract the URL for the outermost archive from the given jar/war URL (which may point to a resource in a jar file or to a jar file itself).

        In the case of a jar file nested within a war file, this will return a URL to the war file since that is the one resolvable in the file system.

        参数:
        jarUrl - the original URL
        返回:
        the URL for the actual jar file
        抛出:
        MalformedURLException - if no valid jar file URL could be extracted
        从以下版本开始:
        4.1.8
        另请参阅:
        extractJarFileURL(URL)
      • toURI

        public static URI toURI​(URL url)
                         throws URISyntaxException
        Create a URI instance for the given URL, replacing spaces with "%20" URI encoding first.
        参数:
        url - the URL to convert into a URI instance
        返回:
        the URI instance
        抛出:
        URISyntaxException - if the URL wasn't a valid URI
        另请参阅:
        URL.toURI()
      • toURI

        public static URI toURI​(String location)
                         throws URISyntaxException
        Create a URI instance for the given location String, replacing spaces with "%20" URI encoding first.
        参数:
        location - the location String to convert into a URI instance
        返回:
        the URI instance
        抛出:
        URISyntaxException - if the location wasn't a valid URI
      • useCachesIfNecessary

        public static void useCachesIfNecessary​(URLConnection con)
        Set the "useCaches" flag on the given connection, preferring false but leaving the flag at true for JNLP based resources.
        参数:
        con - the URLConnection to set the flag on