Section 15.4. From the Java Library: java.net.URL


[Page 715 (continued)]

15.4. From the Java Library: java.net.URL

Given such a URL specification, how can we download its associated resource? Are there Java classes that can help us solve this problem? Fortunately, there are. First, the java.net.URL class contains methods to help retrieve the resource associated with a particular URL (Fig. 15.9). The URL class represents a Uniform Resource Locator. The URL() constructor shown here (there are others) takes a URL specification as a String and, assuming it specifies a valid URL, creates a URL object. If the URL specification is invalid, a MalformedURLException is thrown. A URL would be invalid if the protocol were left off or if it is not a known protocol. The following simple code creates a URL for the home page of our companion Web site:

java.sun.com/docs


URL url; try {   url = new URL("http://www.prenhall.com:80/morelli/index.html"); } catch (MalformedURLException e) {   System.out.println("Malformed URL: " + url.toString()); } 


Figure 15.9. The java.net.URL class.



[Page 716]

Note how we catch the MalformedURLException when we create a new URL.

Once we have a valid URL instance, it can be used to download the data or object associated with it. There are different ways to do this. The openConnection() method creates a URLConnection, which can then be used to download the resource. You would only use this method if your application required extensive control over the download process. A much simpler approach would use the openStream() method. This method will open an InputStream, which you can then use to read the associated URL data the same way you would read a file. This method is especially useful for writing Java applications (rather than applets). As you might guess, downloading Web resources is very easy from a Java applet. Now let's search around for other methods that we can use.

URLs and streams


15.4.1. Code Reuse: The java.applet.Applet Class

The java.applet.Applet class itself contains several useful methods for downloading and displaying Web resources. These methods are inherited by javax.swing.JApplet:

public class Applet extends Panel {     public AppletContext getAppletContext();     public AudioClip getAudioClip(URL url);     public Image getImage(URL url);     public void play(URL url);     public void showStatus(String msg); } 


As you can see, the getImage() and getAudioClip() methods both use a URL to download a resource. An AudioClip is a sound file encoded in AU format, a special type of encoding for sound files. The getImage() method can return files in either GIF or JPEG format, two popular image file formats. The play() method downloads and plays an audio file in one easy step. For example, to download and play an audio clip in an applet requires just two lines of code:

URL url; try {    url = new URL("http://starbase.trincoll.edu/~jjjava/slideshow/sound.au");    play(url); } catch (MalformedURLException e) {      System.out.println("Malformed URL: " + url.toString()) ; } 


Similarly, to download (and store a reference to) an image is just as simple:

URL url; try {    url = new URL("http://starbase.trincoll.edu/~jjjava/slideshow/slide0.gif") ;    imgRef = getImage(url); } catch (MalformedURLException e) {      System.out.println( "Malformed URL: " + url.toString()) ; } 



[Page 717]

It looks as if we have found the methods we need to implement our slide-show applet. We'll use the URL() constructor to create a URL from a String, and we'll use the Applet.getImage(URL) method to retrieve the images from the Web.

What methods can we use?





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net