public class URLUtils extends Object
makeURL is called on the string "gftp://host/file" and
 no gftp handler is installed, it will be interpreted as a file-protocol
 URL referring to the (presumably non-existent) file "gftp://host/file".
 In this case the eventual upshot will presumably be a file-not-found
 type error rather than a MalformedURLException type error getting
 presented to the user.  Users of this class should be of the opinion
 that this is not a particularly bad thing.
 
 The systemId strings used by Sources
 have similar semantics to the strings which this class converts
 to URLs or contexts.
 
This class assumes that the "file:" protocol is legal for URLs, and will throw AssertionErrors if this turns out not to be the case.
| Modifier and Type | Method and Description | 
|---|---|
static URL | 
fixURL(URL url)
Fixes file: URLs which don't have enough slashes in them. 
 | 
static URLConnection | 
followRedirects(URLConnection conn,
               int[] redirCodes)
Takes a URLConnection and repeatedly follows 3xx redirects
 until a non-redirect status is achieved. 
 | 
static void | 
installCustomHandlers()
Attempts to install additional URL protocol handlers suitable 
 for astronomy applications. 
 | 
static URL | 
makeFileURL(File file)
Constructs a legal URL for a given File. 
 | 
static URL | 
makeURL(String location)
Obtains a URL from a string. 
 | 
static URL | 
makeURL(String context,
       String location)
Obtains a URL from a string in a given context. 
 | 
static URL | 
newURL(String spec)
Drop-in replacement for the deprecated
  
URL.URL(String) constructor. | 
static URL | 
resolveLocation(URL url0,
               String location)
Returns the URL corresponding by a supplied location string,
 resolved in the context of a base URL. 
 | 
static boolean | 
sameResource(URL url1,
            URL url2)
Attempts to determine whether two URLs refer to the same resource. 
 | 
static String | 
urlDecode(String txt)
URL-decodes a string using UTF-8, without pesky exceptions. 
 | 
static String | 
urlEncode(String txt)
URL-encodes a string using UTF-8, without pesky exceptions. 
 | 
static boolean | 
urlEquals(URL url1,
         URL url2)
Compares two URLs. 
 | 
static File | 
urlToFile(String url)
Locates the local file, if any, represented by a URL. 
 | 
static URI | 
urlToUri(URL url)
Turns a URL into a URI. 
 | 
public static URL newURL(String spec) throws MalformedURLException
URL.URL(String) constructor.
 All URL constructors are deprecated since Java 20 because of issues with parsing and validation. This utility method provides a way for code to avoid deprecation warnings. It may not do much to solve the underlying problems, and might introduce some new ones, but code that is having problems here can be adapted to handle URL creation more carefully; such approaches, according to the JDK documentation, should generally be URI-based. Other utility methods may be added here in future as required.
As far as I can tell, most of the difficulties arising with URL parsing that have led to the deprecation relate to relatively strange URLs, so that "normal" http/https/file-protocol URL strings passed to this method should behave the same as if passed to the deprecated constructor. However, there may be changes of behaviour when it comes to constructions like embedded spaces in paths or special characters in query parts etc.
Note that passing a string to this method which is not a valid URI,
 for instance because it contains unescaped illegal characters like "[",
 will fail, unlike the call to new URL().
 In such cases a MalformedURLException will be thrown
 (which is really the result of a URISyntaxException).
spec - textual representation of URLMalformedURLException - in case of syntax errorpublic static URL makeURL(String location)
location - a string representing the location of a resourcepublic static URL makeURL(String context, String location)
context is turned into a URL as per 
 the makeURL(String) method, unless it is null or
 the empty string, in which case it is treated as a reference
 to the current directory.
 The string location is then turned into a URL in
 the same way as using makeURL(String), except that
 if it represents a relative path it is resolved in the context
 of context, taking its protocol and/or relative position
 from it.context - a string representing the context within which
                    location is to be resolvedlocation - a string representing the location of a resourcepublic static URI urlToUri(URL url) throws MalformedURLException
Since URIs are syntactically and semantically a superset of
 URLs, this conversion should not cause any errors.  If,
 however, the input URL is malformed in rather extreme ways,
 then the URI construction will fail.  These ways include (but
 are not necesssarily limited to) the features discussed in
 URI.URI(String,String,String,String,String),
 namely that a scheme is present, but with a relative path, or
 that it has a registry-based authority part.
 
Because of the way the class does the conversion, the method will itself resolve some malformations of URLs. You should not rely on this, however, firstly because the method might in principle change, but mostly because you should avoid creating such malformed URLs in the first place.
The most common source of malformed URLs is that of
 file URLs which have inadequately escaped
 (windows) drive letters or spaces in the name: such URLs should
 be constructed using the File.toURI() or File.toURL() methods.  Such URLs will be escaped by
 this method.
url - a URL to be converted.  If this is null, then the
        method returns nullMalformedURLException - if the URI cannot be constructed
        because the input URL turns out to be malformedpublic static URL makeFileURL(File file)
file://localhost/abs-path" rather than 
 "file:abs-or-rel-path".file - filepublic static URL fixURL(URL url)
file:abs-or-rel-path"
 when it should generate "file://localhost/abs-path".url - input URLpublic static boolean sameResource(URL url1, URL url2)
equals.url1 - first URLurl2 - second URLurl1 and url2 appear to
           refer to the same resourcepublic static File urlToFile(String url)
url - URL stringurl, or nullpublic static boolean urlEquals(URL url1, URL url2)
url1 - first URLurl2 - second URLpublic static String urlEncode(String txt)
txt - string to encodeURLEncoderpublic static String urlDecode(String txt)
txt - string to decodeURLDecoderpublic static URLConnection followRedirects(URLConnection conn, int[] redirCodes) throws IOException
Note that the
 HttpURLConnection.setInstanceFollowRedirects(boolean)
 method does something like this, but it refuses to redirect
 between different URL protocols, for security reasons
 (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571).
 Considering similar arguments, this method will direct HTTP->HTTPS,
 but not vice versa.
conn - initial URL connectionredirCodes - list of HTTP codes for which redirects should
                     be followed; if null all suitable 3xx redirections
                     will be followed (301, 302, 303, 307, 308)hconn)IOExceptionpublic static URL resolveLocation(URL url0, String location) throws IOException
url0 - context URLlocation - location, may be relative or absolute URIIOException - if anything goes wrongpublic static void installCustomHandlers()
ivo:" or 
 "myspace:" protocols.Copyright © 2025 Central Laboratory of the Research Councils. All Rights Reserved.