Differences and relations between Java class library URI and URL

In computer terminology, “URI uniform resource identifier” is a purely syntactic structure used to specify different parts of “string identifying network resources”

The format of URI is [scheme:] scheme specific part [#fragment]

schema namespace

scheme specific part is used to identify resources, and the format is defined by the response’s namespace

fragment paragraph

Absolute URI and relative URI

Absolute URI: the complete URI of each component

Relative URI: each component is incomplete and depends on another absolute URI to complete parsing

For example, our common “.”/dir /… “Alternative_ “File” is the relative path

Opaque URI and hierarchical URI

Opaque URI: scheme specific part does not start with a forward slash ‘/’

Hierarchical URI: scheme specific part starts with a forward slash ‘/’, which is most commonly used

The scheme specific part format of hierarchical URI is [//authority] [path] [?query]

authority the specific format is [userinfo @] host [: Port]

userinfo user information

host host, usually IP or domain name

port port number

Path Path

query query data

URL is a subset of URIs, which is the most popular URI. If any resource cannot be located by identifier, it is called “urn unified resource name”. URIs are divided into URL and urn

Here is an example of urn:

mailto:[email protected] 

Now let’s look at the URI and URL classes in the Java class library

URI uri = URI.create(urlString);
URL url = new URL(urlString);

URI uri = url.toURI();
URL url = uri.toURL();

In Java class library, URI class does not contain any methods to access resources, its only function is to parse strings. The URL class provides a way to open a stream to a resource. Therefore, the URL class can only act on the common “schema schema” in Java class library, such as HTTP, HTTPS, FTP, file, jar

Let’s see how to use URI classes:

URI uri = URI.create("http://username:[email protected]:80/path/to/file?p1=v1&p2=v2#hash");
System.out.println(String.format("%s %s %s %s %d %s %s %s",
        uri.getScheme(),
        uri.getAuthority(),
        uri.getUserInfo(),
        uri.getHost(),
        uri.getPort(),
        uri.getPath(),
        uri.getQuery(),
        uri.getFragment()));

// http username:password username:[email protected]:80 www.example.com 80 /path/to/file p1=v1&p2=v2 hash

It should be noted that the URI will automatically encode and decode the strings in it. If you need to obtain the native value, you need to use the getrawxxx() method

In contrast, URL will not encode and decode automatically, and the encoding and decoding provided by urlencoder/urldecoder class is only applicable to HTML, which is different from the encoding mechanism defined in rfc2396

Uri class also provides conversion and parsing of relative path and absolute path. Please try to play by yourself

Similar Posts: