Downloading a File in an Applet or Application
Write a java application or applet that downloads a file from across the network.
Due to security considerations, applets may only download files on the server from
which they were downloaded. To download a file, you must construct the URL location
as an instance of the URL class. With the URL class in hand, you can invoke the
openConnection() method to retrieve an instance of the URLConnection class.
From here there are two options:
Added Sophistication:
- Write a viewer for the file you intend to download and launch the viewer when the file is downloaded.
The file shouldn't be an image, because image downloading is already implemented.
- For an application, write a directory browser that allows the user to select from appropriate
files in a directory. For an applet, write an interface to select from file parameterized in the html
applet tag. When a connection is made to a URL that is a directory, a directory listing in html is returned.
Script to run the Download demo
References
Flanagan, David; Java in a Nutshell, Section 7 on Networking
The Java tutorial is a
useful online resource, especially the chapters on
networking overview and working with URLs.
Look at the Javasoft Java API Documentation on the
java.net package.