About Armando Flores

Armando graduated from from Electronics Engineer in the The Public University Of Puebla (BUAP). He also has a Masters degree in Computer Sciences from CINVESTAV. He has been using the Java language for Web Development for over a decade. He has been involved in a large number of projects focused on "ad-hoc" Web Application based on Java EE and Spring Framework.

java.net.URL Example

In this example, we will show the range of functionality provided by the java.net.URL class. This class represents a Uniform Resource Locator, a pointer to a “resource” on the World Wide Web.

An URL is a text string that identifies a resource, tells where to find it, and specifies a method for communicating with it or retrieving it from its source. URLs can have many forms. The most common form has four components; a network host or server, the name of the resource, its location on that host, and a protocol by which the host should communicate: protocol://hostname/path/resource-name.

protocol is an identifier such as http or ftp; hostname is usually an Internet host and domain name; and the path and resource-name components form a unique path that identifies the object on that host.

Let’s code one of the most common task for which java.net.URL class is used: Read a file using the http protocol.

(String protocol, String host, int port, String file) – Creates a URL object from the specified protocol, host, port number, and file. host can be expressed as a host name or a literal IP address. Specifying a port number of -1 indicates that the URL should use the default port for the protocol.

If we run the above code, we will get the following results:

URL1: http://www.gnu.org
URL2: http://www.gnu.org/licenses/gpl.txt
URL3: http://www.gnu.org/licenses/gpl.txt
URL4: http://www.gnu.org:80/licenses/gpl.txt
/***** File content (URL4) *****/
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.

getDefaultPort() – Gets the default port number of the protocol associated with this URL. If the URL scheme or the URLStreamHandler for the URL do not define a default port number, then -1 is returned.

getFile() – Gets the file name of this URL, or an empty string if one does not exist.

getHost() – Gets the host name of this URL, if applicable. The format of the host conforms to RFC2732, i.e. for a literal IPv6 address, this method will return the IPv6 address enclosed in square brackets (‘[‘ and ‘]’).

getPath() – Gets the path part of this URL, or an empty string if one does not exist.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.