|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2023 the original author or authors. |
|
|
|
* Copyright 2002-2024 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -34,7 +34,7 @@ import org.springframework.lang.Nullable; |
|
|
|
* <p>Consider using Spring's Resource abstraction in the core package
|
|
|
|
* <p>Consider using Spring's Resource abstraction in the core package
|
|
|
|
* for handling all kinds of file resources in a uniform manner. |
|
|
|
* for handling all kinds of file resources in a uniform manner. |
|
|
|
* {@link org.springframework.core.io.ResourceLoader}'s {@code getResource()} |
|
|
|
* {@link org.springframework.core.io.ResourceLoader}'s {@code getResource()} |
|
|
|
* method can resolve any location to an {@link org.springframework.core.io.Resource} |
|
|
|
* method can resolve any location to a {@link org.springframework.core.io.Resource} |
|
|
|
* object, which in turn allows one to obtain a {@code java.io.File} in the |
|
|
|
* object, which in turn allows one to obtain a {@code java.io.File} in the |
|
|
|
* file system through its {@code getFile()} method. |
|
|
|
* file system through its {@code getFile()} method. |
|
|
|
* |
|
|
|
* |
|
|
|
@ -101,6 +101,7 @@ public abstract class ResourceUtils { |
|
|
|
* @return whether the location qualifies as a URL |
|
|
|
* @return whether the location qualifies as a URL |
|
|
|
* @see #CLASSPATH_URL_PREFIX |
|
|
|
* @see #CLASSPATH_URL_PREFIX |
|
|
|
* @see java.net.URL |
|
|
|
* @see java.net.URL |
|
|
|
|
|
|
|
* @see #toURL(String) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean isUrl(@Nullable String resourceLocation) { |
|
|
|
public static boolean isUrl(@Nullable String resourceLocation) { |
|
|
|
if (resourceLocation == null) { |
|
|
|
if (resourceLocation == null) { |
|
|
|
@ -126,6 +127,7 @@ public abstract class ResourceUtils { |
|
|
|
* "classpath:" pseudo URL, a "file:" URL, or a plain file path |
|
|
|
* "classpath:" pseudo URL, a "file:" URL, or a plain file path |
|
|
|
* @return a corresponding URL object |
|
|
|
* @return a corresponding URL object |
|
|
|
* @throws FileNotFoundException if the resource cannot be resolved to a URL |
|
|
|
* @throws FileNotFoundException if the resource cannot be resolved to a URL |
|
|
|
|
|
|
|
* @see #toURL(String) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static URL getURL(String resourceLocation) throws FileNotFoundException { |
|
|
|
public static URL getURL(String resourceLocation) throws FileNotFoundException { |
|
|
|
Assert.notNull(resourceLocation, "Resource location must not be null"); |
|
|
|
Assert.notNull(resourceLocation, "Resource location must not be null"); |
|
|
|
@ -166,6 +168,7 @@ public abstract class ResourceUtils { |
|
|
|
* @return a corresponding File object |
|
|
|
* @return a corresponding File object |
|
|
|
* @throws FileNotFoundException if the resource cannot be resolved to |
|
|
|
* @throws FileNotFoundException if the resource cannot be resolved to |
|
|
|
* a file in the file system |
|
|
|
* a file in the file system |
|
|
|
|
|
|
|
* @see #getFile(URL) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static File getFile(String resourceLocation) throws FileNotFoundException { |
|
|
|
public static File getFile(String resourceLocation) throws FileNotFoundException { |
|
|
|
Assert.notNull(resourceLocation, "Resource location must not be null"); |
|
|
|
Assert.notNull(resourceLocation, "Resource location must not be null"); |
|
|
|
@ -197,6 +200,7 @@ public abstract class ResourceUtils { |
|
|
|
* @return a corresponding File object |
|
|
|
* @return a corresponding File object |
|
|
|
* @throws FileNotFoundException if the URL cannot be resolved to |
|
|
|
* @throws FileNotFoundException if the URL cannot be resolved to |
|
|
|
* a file in the file system |
|
|
|
* a file in the file system |
|
|
|
|
|
|
|
* @see #getFile(URL, String) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static File getFile(URL resourceUrl) throws FileNotFoundException { |
|
|
|
public static File getFile(URL resourceUrl) throws FileNotFoundException { |
|
|
|
return getFile(resourceUrl, "URL"); |
|
|
|
return getFile(resourceUrl, "URL"); |
|
|
|
@ -237,6 +241,7 @@ public abstract class ResourceUtils { |
|
|
|
* @throws FileNotFoundException if the URL cannot be resolved to |
|
|
|
* @throws FileNotFoundException if the URL cannot be resolved to |
|
|
|
* a file in the file system |
|
|
|
* a file in the file system |
|
|
|
* @since 2.5 |
|
|
|
* @since 2.5 |
|
|
|
|
|
|
|
* @see #getFile(URI, String) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static File getFile(URI resourceUri) throws FileNotFoundException { |
|
|
|
public static File getFile(URI resourceUri) throws FileNotFoundException { |
|
|
|
return getFile(resourceUri, "URI"); |
|
|
|
return getFile(resourceUri, "URI"); |
|
|
|
@ -268,6 +273,7 @@ public abstract class ResourceUtils { |
|
|
|
* i.e. has protocol "file", "vfsfile" or "vfs". |
|
|
|
* i.e. has protocol "file", "vfsfile" or "vfs". |
|
|
|
* @param url the URL to check |
|
|
|
* @param url the URL to check |
|
|
|
* @return whether the URL has been identified as a file system URL |
|
|
|
* @return whether the URL has been identified as a file system URL |
|
|
|
|
|
|
|
* @see #isJarURL(URL) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean isFileURL(URL url) { |
|
|
|
public static boolean isFileURL(URL url) { |
|
|
|
String protocol = url.getProtocol(); |
|
|
|
String protocol = url.getProtocol(); |
|
|
|
@ -281,6 +287,7 @@ public abstract class ResourceUtils { |
|
|
|
* "vfszip", or "wsjar". |
|
|
|
* "vfszip", or "wsjar". |
|
|
|
* @param url the URL to check |
|
|
|
* @param url the URL to check |
|
|
|
* @return whether the URL has been identified as a JAR URL |
|
|
|
* @return whether the URL has been identified as a JAR URL |
|
|
|
|
|
|
|
* @see #isJarFileURL(URL) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean isJarURL(URL url) { |
|
|
|
public static boolean isJarURL(URL url) { |
|
|
|
String protocol = url.getProtocol(); |
|
|
|
String protocol = url.getProtocol(); |
|
|
|
@ -295,6 +302,7 @@ public abstract class ResourceUtils { |
|
|
|
* @param url the URL to check |
|
|
|
* @param url the URL to check |
|
|
|
* @return whether the URL has been identified as a JAR file URL |
|
|
|
* @return whether the URL has been identified as a JAR file URL |
|
|
|
* @since 4.1 |
|
|
|
* @since 4.1 |
|
|
|
|
|
|
|
* @see #extractJarFileURL(URL) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static boolean isJarFileURL(URL url) { |
|
|
|
public static boolean isJarFileURL(URL url) { |
|
|
|
return (URL_PROTOCOL_FILE.equals(url.getProtocol()) && |
|
|
|
return (URL_PROTOCOL_FILE.equals(url.getProtocol()) && |
|
|
|
@ -307,6 +315,7 @@ public abstract class ResourceUtils { |
|
|
|
* @param jarUrl the original URL |
|
|
|
* @param jarUrl the original URL |
|
|
|
* @return the URL for the actual jar file |
|
|
|
* @return the URL for the actual jar file |
|
|
|
* @throws MalformedURLException if no valid jar file URL could be extracted |
|
|
|
* @throws MalformedURLException if no valid jar file URL could be extracted |
|
|
|
|
|
|
|
* @see #extractArchiveURL(URL) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException { |
|
|
|
public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException { |
|
|
|
String urlFile = jarUrl.getFile(); |
|
|
|
String urlFile = jarUrl.getFile(); |
|
|
|
@ -368,6 +377,7 @@ public abstract class ResourceUtils { |
|
|
|
* @return the URI instance |
|
|
|
* @return the URI instance |
|
|
|
* @throws URISyntaxException if the URL wasn't a valid URI |
|
|
|
* @throws URISyntaxException if the URL wasn't a valid URI |
|
|
|
* @see java.net.URL#toURI() |
|
|
|
* @see java.net.URL#toURI() |
|
|
|
|
|
|
|
* @see #toURI(String) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static URI toURI(URL url) throws URISyntaxException { |
|
|
|
public static URI toURI(URL url) throws URISyntaxException { |
|
|
|
return toURI(url.toString()); |
|
|
|
return toURI(url.toString()); |
|
|
|
@ -379,18 +389,21 @@ public abstract class ResourceUtils { |
|
|
|
* @param location the location String to convert into a URI instance |
|
|
|
* @param location the location String to convert into a URI instance |
|
|
|
* @return the URI instance |
|
|
|
* @return the URI instance |
|
|
|
* @throws URISyntaxException if the location wasn't a valid URI |
|
|
|
* @throws URISyntaxException if the location wasn't a valid URI |
|
|
|
|
|
|
|
* @see #toURI(URL) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static URI toURI(String location) throws URISyntaxException { |
|
|
|
public static URI toURI(String location) throws URISyntaxException { |
|
|
|
return new URI(StringUtils.replace(location, " ", "%20")); |
|
|
|
return new URI(StringUtils.replace(location, " ", "%20")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a URL instance for the given location String, |
|
|
|
* Create a clean URL instance for the given location String, |
|
|
|
* going through URI construction and then URL conversion. |
|
|
|
* going through URI construction and then URL conversion. |
|
|
|
* @param location the location String to convert into a URL instance |
|
|
|
* @param location the location String to convert into a URL instance |
|
|
|
* @return the URL instance |
|
|
|
* @return the URL instance |
|
|
|
* @throws MalformedURLException if the location wasn't a valid URL |
|
|
|
* @throws MalformedURLException if the location wasn't a valid URL |
|
|
|
* @since 6.0 |
|
|
|
* @since 6.0 |
|
|
|
|
|
|
|
* @see java.net.URI#toURL() |
|
|
|
|
|
|
|
* @see #toURI(String) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("deprecation") // on JDK 20
|
|
|
|
@SuppressWarnings("deprecation") // on JDK 20
|
|
|
|
public static URL toURL(String location) throws MalformedURLException { |
|
|
|
public static URL toURL(String location) throws MalformedURLException { |
|
|
|
@ -406,13 +419,15 @@ public abstract class ResourceUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a URL instance for the given root URL and relative path, |
|
|
|
* Create a clean URL instance for the given root URL and relative path, |
|
|
|
* going through URI construction and then URL conversion. |
|
|
|
* going through URI construction and then URL conversion. |
|
|
|
* @param root the root URL to start from |
|
|
|
* @param root the root URL to start from |
|
|
|
* @param relativePath the relative path to apply |
|
|
|
* @param relativePath the relative path to apply |
|
|
|
* @return the relative URL instance |
|
|
|
* @return the relative URL instance |
|
|
|
* @throws MalformedURLException if the end result is not a valid URL |
|
|
|
* @throws MalformedURLException if the end result is not a valid URL |
|
|
|
* @since 6.0 |
|
|
|
* @since 6.0 |
|
|
|
|
|
|
|
* @see #toURL(String) |
|
|
|
|
|
|
|
* @see StringUtils#applyRelativePath |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException { |
|
|
|
public static URL toRelativeURL(URL root, String relativePath) throws MalformedURLException { |
|
|
|
// # can appear in filenames, java.net.URL should not treat it as a fragment
|
|
|
|
// # can appear in filenames, java.net.URL should not treat it as a fragment
|
|
|
|
@ -426,6 +441,7 @@ public abstract class ResourceUtils { |
|
|
|
* given connection, preferring {@code false} but leaving the flag at |
|
|
|
* given connection, preferring {@code false} but leaving the flag at |
|
|
|
* its JVM default value for jar resources (typically {@code true}). |
|
|
|
* its JVM default value for jar resources (typically {@code true}). |
|
|
|
* @param con the URLConnection to set the flag on |
|
|
|
* @param con the URLConnection to set the flag on |
|
|
|
|
|
|
|
* @see URLConnection#setUseCaches |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void useCachesIfNecessary(URLConnection con) { |
|
|
|
public static void useCachesIfNecessary(URLConnection con) { |
|
|
|
if (!(con instanceof JarURLConnection)) { |
|
|
|
if (!(con instanceof JarURLConnection)) { |
|
|
|
|