Java Url Processing
π
2026-06-18 | π Java
Java URL Processing
URL (Uniform Resource Locator), known in Chinese as "Unified Resource Locator" and sometimes colloquially referred to as a web address, represents a resource on the internet, such as a webpage or an FTP address.
In this chapter, we will introduce how Java handles URLs. A URL can be divided into the following parts:
protocol://host:port/path?query#fragment
The protocol can be HTTP, HTTPS, FTP, or File. The port is the port number, and the path is the file path and filename.
An example of an HTTP protocol URL is:
URL parsing:
* **Protocol:** http
* **Host (host:port):** example.com
* **Port:** 80. The above URL example does not specify a port because the default port for the HTTP protocol is 80.
* **Path:** /index.html
* **Query:** language=cn
* **Fragment:** j2se, which locates the HTML element with the id attribute "j2se" on the webpage.
* * *
## URL Class Methods
The URL class is defined in the java.net package and is used to handle URL-related content. The creation and usage of the URL class are introduced below.
java.net.URL provides rich methods for constructing URLs and can be used to access resources.
| No. | Method Description |
| --- | --- |
| 1 | **public URL(String protocol, String host, int port, String file) throws MalformedURLException.** Creates a URL using the given parameters (protocol, hostname, port number, filename). |
| 2 | **public URL(String protocol, String host, String file) throws MalformedURLException** Creates a URL using the specified protocol, hostname, and filename, with the default port for the protocol. |
| 3 | **public URL(String url) throws MalformedURLException** Creates a URL from the given URL string. |
| 4 | **public URL(URL context, String url) throws MalformedURLException** Creates a URL using a base address and a relative URL. |
The URL class contains many methods for accessing various parts of a URL. The specific methods and their descriptions are as follows:
| No. | Method Description |
| --- | --- |
| 1 | **public String getPath()** Returns the path part of the URL. |
| 2 | **public String getQuery()** Returns the query part of the URL. |
| 3 | **public String getAuthority()** Returns the authority part of this URL. |
| 4 | **public int getPort()** Returns the port part of the URL. |
| 5 | **public int getDefaultPort()** Returns the default port number for the protocol. |
| 6 | **public String getProtocol()** Returns the protocol of the URL. |
| 7 | **public String getHost()** Returns the host of the URL. |
| 8 | **public String getFile()** Returns the filename part of the URL. |
| 9 | **public String getRef()** Returns the anchor (also known as the "reference") of this URL. |
| 10 | **public URLConnection openConnection() throws IOException** Opens a URL connection and allows client access to the resource. |
### Example
The following example demonstrates using the URL class from java.net to obtain various parameters of a URL:
## URLDemo.java
```java
import java.net.*;
import java.io.*;
public class URLDemo {
public static void main(String[] args) {
try {
URL url = new URL("");
System.out.println("URL: " + url.toString());
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Authority: " + url.getAuthority());
System.out.println("File name & Query parameter: " + url.getFile());
System.out.println("Host: " + url.getHost());
System.out.println("Path: " + url.getPath());
System.out.println("Port: " + url.getPort());
System.out.println("Default port: " + url.getDefaultPort());
System.out.println("Query parameter: " + url.getQuery());
System.out.println("Ref: " + url.getRef());
} catch (IOException e) {
e.printStackTrace();
}
}
}
The compilation and execution results of the above example are as follows:
URL:
Protocol: http
Authority: example.com
File name & Query parameter: /index.html?language=cn
Host: example.com
Path: /index.html
Port: -1
Default port: 80
Query parameter: language=cn
Ref: j2se
* * *
## URLConnection Class Methods
The openConnection() method returns a java.net.URLConnection.
For example:
* If you connect to a URL using the HTTP protocol, the openConnection() method returns an HttpURLConnection object.
* If the URL you connect to is a JAR file, the openConnection() method will return a JarURLConnection object.
* And so on...
The URLConnection methods are listed below:
| No. | Method Description |
| --- | --- |
| 1 | **Object getContent()** Retrieves the content of the URL link. |
| 2 | **Object getContent(Class[] classes)** Retrieves the content of the URL link. |
| 3 | **String getContentEncoding()** Returns the value of the content-encoding header field. |
| 4 | **int getContentLength()** Returns the value of the content-length header field. |
| 5 | **String getContentType()** Returns the value of the content-type header field. |
| 6 | **int getLastModified()** Returns the value of the last-modified header field. |
| 7 | **long getExpiration()** Returns the value of the expires header field. |
| 8 | **long getIfModifiedSince()** Returns the value of the ifModifiedSince field of the object. |
| 9 | **public void setDoInput(boolean input)** A URL connection can be used for input and/or output. If you intend to use the URL connection for input, set the DoInput flag to true; otherwise, set it to false. The default value is true. |
| 10 | **public void setDoOutput(boolean output)** A URL connection can be used for input and/or output. If you intend to use the URL connection for output, set the DoOutput flag to true; otherwise, set it to false. The default value is false. |
| 11 | **public InputStream getInputStream() throws IOException** Returns the input stream of the URL, used for reading resources. |
| 12 | **public OutputStream getOutputStream() throws IOException** Returns the output stream of the URL, used for writing resources. |
| 13 | **public URL getURL()** Returns the URL to which the URLConnection object is connected. |
### Example
In the following example, the URL uses the HTTP protocol. openConnection returns an HttpURLConnection object.
## URLConnDemo.java
```java
import java.net.*;
import java.io.*;
public class URLConnDemo {
public static void main(String[] args) {
try {
URL url = new URL("");
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = null;
if (urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
} else {
System.out.println("Please enter a URL address");
return;
}
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while ((current = in.readLine()) != null) {
urlString += current;
}
System.out.println(urlString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The compilation and execution results of the above example are as follows:
$ javac URLConnDemo.java
$ java URLConnDemo
.....This will output the HTML content of the Tutorial homepage (#).....