Excerpt from: http://www.runoob.com/java/java-url-processing.html
Java URL Processing
URL (Uniform Resource Locator) Chinese name is Uniform Resource Locator, sometimes also known as the Web address. Represents a resource on the Internet, such as a Web page or an FTP address.
In this section we will describe how Java handles URLs. The URL can be divided into the following sections.
Protocol://host:port/path?query#fragment
The Protocol (Protocol) can be HTTP, HTTPS, FTP, and file, port is the port number, and path is the file path and filename.
The URL instance of the HTTP protocol is as follows:
http://www.runoob.com/index.html?language=cn#j2se
URL parsing:
- Protocol (Protocol):http
- Host (Host:port):www.runoob.com
- Port Number (port):80, the above URL does not specify a port because the default port for the HTTP protocol is 80
- file Path (path):/index.html
- request parameter (query): lANGUAGE=CN
- anchor Position (fragment):j2se, navigate to the HTML element location in the page with the id attribute j2se
URL class method:
A URL class is defined in the java.net package that is used to process the contents of the URL. For the creation and use of URL classes, the following are described separately.
Java.net.URL Construction Method:
Serial number |
Method description |
1 |
Public URL (String protocol,string host,int port,string file) throws Malformedurlexception Create a URL with a given parameter (protocol, hostname, port number, file name) |
2 |
Public URL (String protocal,string host,string file) throws Malformedurlexception Creates a URL using the given parameters (Protocol, hostname, file name), using the default 80 port number |
3 |
Public url (String URL) throws Malformedurlexception Create a URL from a given string |
4 |
Public url (url context,string url) throws Malformedurlexception Create using base address and relative URL |
The URL class contains a number of methods for accessing the various parts of the URL, in the following ways and descriptions:
Serial number |
Method |
Describe |
1 |
Public String GetPath () |
Return to the URL path section |
2 |
Public String Getquery () |
Return to the URL query section |
3 |
Public String getauthority () |
Get this URL authorization section |
4 |
public int Getport () |
Return to the URL port section |
5 |
public int Getdefaultport () |
Returns the default port number for the protocol |
6 |
Public String Getprotocol () |
The protocol that returns the URL |
7 |
Public String GetHost () |
Returns the host of the URL |
8 |
Public String GetFile () |
Returns the URL file name section |
9 |
Public String GetRef () |
Returns the anchor point of the URL (also referred to as a "reference") |
10 |
Public URLConnection OpenConnection () Throws IOException |
Open a URL connection and run the Client Access resource |
Instance
The following example shows the URL class that uses java.net to get the various parts of the URL parameter:
Importjava.net.*;ImportJava.io.*; Public classurldemo{ Public Static voidmain (String [] args) {Try{URL URL=NewURL ("Http://www.runoob.com/index.html?language=cn#j2se"); System.out.println ("URL is:" +url.tostring ()); System.out.println (The agreement is: "+Url.getprotocol ()); System.out.println ("Verification Information:" +url.getauthority ()); System.out.println ("File name and request parameters:" +url.getfile ()); System.out.println ("Host Name:" +url.gethost ()); System.out.println ("Path:" +Url.getpath ()); System.out.println ("Port:" +Url.getport ()); System.out.println ("Default port:" +Url.getdefaultport ()); System.out.println ("Request Parameters:" +url.getquery ()); System.out.println ("Positioning Location:" +url.getref ()); }Catch(IOException e) {e.printstacktrace (); } }}
Urlconnections class method
OpenConnection () returns a java.net.URLConnection.
For example:
If you connect the URL of the HTTP protocol, the OpenConnection () method returns the HttpURLConnection object.
If the URL you are connecting to is a JAR file, the OpenConnection () method returns the Jarurlconnection object.
Wait a minute...
The list of URLConnection methods is as follows:
Serial Number |
Method Description |
1 |
Object getcontent () Retrieving URL link content |
2 |
Object getcontent (class[] classes) Retrieving URL link content |
3 |
String getcontentencoding () Returns the header content-encoding field value. |
4 |
int getcontentlength () Return header content-length field value |
5 |
String getcontenttype () Return header Content-type field value |
6 |
int getlastmodified () Returns the header last-modified field value. |
7 |
Long getexpiration () Returns the header Expires field value. |
8 |
Long getifmodifiedsince () Returns the Ifmodifiedsince field value of an object. |
9 |
public void Setdoinput (Boolean input) URL connections can be used for input and/or output. If you intend to use a URL connection for input, set the DOINPUT flag to true, or set to False if you do not intend to use it. The default value is true. |
10 |
public void Setdooutput (Boolean output) URL connections can be used for input and/or output. If you intend to use a URL connection for output, set the DOOUTPUT flag to true, or set to False if you do not intend to use it. The default value is False. |
11 |
Public InputStream getInputStream () throws IOException Returns the input stream of the URL used to read the resource |
12 |
Public OutputStream Getoutputstream () throws IOException Returns the output stream of the URL for writing to the resource. |
13 |
Public URL GetURL () Returns the URL of the URLConnection object connection |
Instance
The URL in the following instance uses the HTTP protocol. OpenConnection returns the HttpURLConnection object.
Importjava.net.*;ImportJava.io.*; Public classurlconndemo{ Public Static voidmain (String [] args) {Try{URL URL=NewURL ("http://www.runoob.com"); URLConnection URLConnection=url.openconnection (); HttpURLConnection Connection=NULL; if(URLConnectioninstanceofhttpurlconnection) {Connection=(httpurlconnection) URLConnection; } Else{System.out.println ("Please enter the URL address"); return; } BufferedReader in=NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); String urlstring= ""; String current; while(current = In.readline ())! =NULL) {urlstring+=Current ; } System.out.println (urlstring); }Catch(IOException e) {e.printstacktrace (); } }}
Java Tour _ Advanced Tutorial _url Processing