1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
34
34
* <p>Consider using Spring's Resource abstraction in the core package
35
35
* for handling all kinds of file resources in a uniform manner.
36
36
* {@link org.springframework.core.io.ResourceLoader}'s {@code getResource()}
37
- * method can resolve any location to an {@link org.springframework.core.io.Resource}
37
+ * method can resolve any location to a {@link org.springframework.core.io.Resource}
38
38
* object, which in turn allows one to obtain a {@code java.io.File} in the
39
39
* file system through its {@code getFile()} method.
40
40
*
@@ -101,6 +101,7 @@ public abstract class ResourceUtils {
101
101
* @return whether the location qualifies as a URL
102
102
* @see #CLASSPATH_URL_PREFIX
103
103
* @see java.net.URL
104
+ * @see #toURL(String)
104
105
*/
105
106
public static boolean isUrl (@ Nullable String resourceLocation ) {
106
107
if (resourceLocation == null ) {
@@ -126,6 +127,7 @@ public static boolean isUrl(@Nullable String resourceLocation) {
126
127
* "classpath:" pseudo URL, a "file:" URL, or a plain file path
127
128
* @return a corresponding URL object
128
129
* @throws FileNotFoundException if the resource cannot be resolved to a URL
130
+ * @see #toURL(String)
129
131
*/
130
132
public static URL getURL (String resourceLocation ) throws FileNotFoundException {
131
133
Assert .notNull (resourceLocation , "Resource location must not be null" );
@@ -166,6 +168,7 @@ public static URL getURL(String resourceLocation) throws FileNotFoundException {
166
168
* @return a corresponding File object
167
169
* @throws FileNotFoundException if the resource cannot be resolved to
168
170
* a file in the file system
171
+ * @see #getFile(URL)
169
172
*/
170
173
public static File getFile (String resourceLocation ) throws FileNotFoundException {
171
174
Assert .notNull (resourceLocation , "Resource location must not be null" );
@@ -197,6 +200,7 @@ public static File getFile(String resourceLocation) throws FileNotFoundException
197
200
* @return a corresponding File object
198
201
* @throws FileNotFoundException if the URL cannot be resolved to
199
202
* a file in the file system
203
+ * @see #getFile(URL, String)
200
204
*/
201
205
public static File getFile (URL resourceUrl ) throws FileNotFoundException {
202
206
return getFile (resourceUrl , "URL" );
@@ -237,6 +241,7 @@ public static File getFile(URL resourceUrl, String description) throws FileNotFo
237
241
* @throws FileNotFoundException if the URL cannot be resolved to
238
242
* a file in the file system
239
243
* @since 2.5
244
+ * @see #getFile(URI, String)
240
245
*/
241
246
public static File getFile (URI resourceUri ) throws FileNotFoundException {
242
247
return getFile (resourceUri , "URI" );
@@ -268,6 +273,7 @@ public static File getFile(URI resourceUri, String description) throws FileNotFo
268
273
* i.e. has protocol "file", "vfsfile" or "vfs".
269
274
* @param url the URL to check
270
275
* @return whether the URL has been identified as a file system URL
276
+ * @see #isJarURL(URL)
271
277
*/
272
278
public static boolean isFileURL (URL url ) {
273
279
String protocol = url .getProtocol ();
@@ -281,6 +287,7 @@ public static boolean isFileURL(URL url) {
281
287
* "vfszip", or "wsjar".
282
288
* @param url the URL to check
283
289
* @return whether the URL has been identified as a JAR URL
290
+ * @see #isJarFileURL(URL)
284
291
*/
285
292
public static boolean isJarURL (URL url ) {
286
293
String protocol = url .getProtocol ();
@@ -295,6 +302,7 @@ public static boolean isJarURL(URL url) {
295
302
* @param url the URL to check
296
303
* @return whether the URL has been identified as a JAR file URL
297
304
* @since 4.1
305
+ * @see #extractJarFileURL(URL)
298
306
*/
299
307
public static boolean isJarFileURL (URL url ) {
300
308
return (URL_PROTOCOL_FILE .equals (url .getProtocol ()) &&
@@ -307,6 +315,7 @@ public static boolean isJarFileURL(URL url) {
307
315
* @param jarUrl the original URL
308
316
* @return the URL for the actual jar file
309
317
* @throws MalformedURLException if no valid jar file URL could be extracted
318
+ * @see #extractArchiveURL(URL)
310
319
*/
311
320
public static URL extractJarFileURL (URL jarUrl ) throws MalformedURLException {
312
321
String urlFile = jarUrl .getFile ();
@@ -368,6 +377,7 @@ public static URL extractArchiveURL(URL jarUrl) throws MalformedURLException {
368
377
* @return the URI instance
369
378
* @throws URISyntaxException if the URL wasn't a valid URI
370
379
* @see java.net.URL#toURI()
380
+ * @see #toURI(String)
371
381
*/
372
382
public static URI toURI (URL url ) throws URISyntaxException {
373
383
return toURI (url .toString ());
@@ -379,18 +389,21 @@ public static URI toURI(URL url) throws URISyntaxException {
379
389
* @param location the location String to convert into a URI instance
380
390
* @return the URI instance
381
391
* @throws URISyntaxException if the location wasn't a valid URI
392
+ * @see #toURI(URL)
382
393
*/
383
394
public static URI toURI (String location ) throws URISyntaxException {
384
395
return new URI (StringUtils .replace (location , " " , "%20" ));
385
396
}
386
397
387
398
/**
388
- * Create a URL instance for the given location String,
399
+ * Create a clean URL instance for the given location String,
389
400
* going through URI construction and then URL conversion.
390
401
* @param location the location String to convert into a URL instance
391
402
* @return the URL instance
392
403
* @throws MalformedURLException if the location wasn't a valid URL
393
404
* @since 6.0
405
+ * @see java.net.URI#toURL()
406
+ * @see #toURI(String)
394
407
*/
395
408
@ SuppressWarnings ("deprecation" ) // on JDK 20
396
409
public static URL toURL (String location ) throws MalformedURLException {
@@ -406,13 +419,15 @@ public static URL toURL(String location) throws MalformedURLException {
406
419
}
407
420
408
421
/**
409
- * Create a URL instance for the given root URL and relative path,
422
+ * Create a clean URL instance for the given root URL and relative path,
410
423
* going through URI construction and then URL conversion.
411
424
* @param root the root URL to start from
412
425
* @param relativePath the relative path to apply
413
426
* @return the relative URL instance
414
427
* @throws MalformedURLException if the end result is not a valid URL
415
428
* @since 6.0
429
+ * @see #toURL(String)
430
+ * @see StringUtils#applyRelativePath
416
431
*/
417
432
public static URL toRelativeURL (URL root , String relativePath ) throws MalformedURLException {
418
433
// # can appear in filenames, java.net.URL should not treat it as a fragment
@@ -426,6 +441,7 @@ public static URL toRelativeURL(URL root, String relativePath) throws MalformedU
426
441
* given connection, preferring {@code false} but leaving the flag at
427
442
* its JVM default value for jar resources (typically {@code true}).
428
443
* @param con the URLConnection to set the flag on
444
+ * @see URLConnection#setUseCaches
429
445
*/
430
446
public static void useCachesIfNecessary (URLConnection con ) {
431
447
if (!(con instanceof JarURLConnection )) {
0 commit comments