You developed client/server applications in the preceding sections. Java allows you to develop clients that retrieve files on a remote host through a Web server. In this case, you don't have to create a custom server program. The Web server can be used to send the files, as shown in Figure 25.12.
To retrieve a file, first create a URL object for the file. The java.net.URL class was introduced in §16.9, "Locating Resource Using the URL Class." You can create a URL object using the following constructor:
public URL(String spec) throws MalformedURLException
For example, the statement given below creates a URL object for http://www.cs.armstrong.edu/liang/index.html:
try { URL url = new URL( "http://www.cs.armstrong.edu/liang/index.html" ); } catch (MalformedURLException ex) { }
A MalformedURLException is thrown if the URL string has a syntax error. For example, the URL string "http://www.cs.armstrong.edu/liang/index.html" would cause a Malformed-URLException runtime error because two slashes ( // ) are required after the colon ( : ).
You can then use the openStream() method defined in the URL class to open an input stream to the file's URL.
InputStream inputStream = url.openStream();
Now you can read the data from the input stream. Listing 25.10 gives an example that demonstrates how to retrieve a file from a Web server. The program can run as an application or an applet. The user interface includes a text field in which to enter the URL of the filename, a text area in which to show the file, and a button that can be used to submit an action. A label is added at the bottom of the applet to indicate the status, such as File loaded successfully or Network connection problem . A sample run of the program is shown in Figure 25.13.
1 import java.awt.*; 2 import java.awt.event.*; 3 import java.io.*; 4 import java.net.*; 5 import javax.swing.*; 6 7 public class ViewRemoteFile extends JApplet { 8 // Button to view the file 9 private JButton jbtView = new JButton( "View" ); 10 11 // Text field to receive file name 12 private JTextField jtfURL = new JTextField( 12 ); 13 14 // Text area to store file 15 private JTextArea jtaFile = new JTextArea(); 16 17 // Label to display status 18 private JLabel jlblStatus = new JLabel(); 19 20 /** Initialize the applet */ 21 public void init() { 22 // Create a panel to hold a label, a text field, and a button 23 JPanel p1 = new JPanel(); 24 p1.setLayout( new BorderLayout()); 25 p1.add( new JLabel( "Filename" ), BorderLayout.WEST); 26 p1.add(jtfURL, BorderLayout.CENTER); 27 p1.add(jbtView, BorderLayout.EAST); 28 29 // Place text area and panel p to the applet 30 setLayout( new BorderLayout()); 31 add( new JScrollPane(jtaFile), BorderLayout.CENTER); 32 add(p1, BorderLayout.NORTH); 33 add(jlblStatus, BorderLayout.SOUTH); 34 35 // Register listener to handle the "View" button 36 jbtView.addActionListener( new ActionListener() { 37 public void actionPerformed(ActionEvent e) { 38 showFile(); 39 } 40 }); 41 } 42 43 private void showFile() { 44 // Declare buffered stream for reading text for the URL 45 BufferedReader infile = null ; 46 URL url = null ; 47 48 try { 49 // Obtain URL from the text field 50 url = new URL(jtfURL.getText().trim()); 51 52 // Create a buffered stream 53 InputStream is = url.openStream(); 54 infile = new BufferedReader( new InputStreamReader(is) ); 55 56 // Get file name from the text field 57 String inLine; 58 59 // Read a line and append the line to the text area 60 while ((inLine = infile.readLine()) != null ) { 61 jtaFile.append(inLine + '\n' ); 62 } 63 64 jlblStatus.setText( "File loaded successfully" ); 65 } 66 catch (FileNotFoundException e) { 67 jlblStatus.setText( "URL " + url + " not found." ); 68 } 69 catch (IOException e) { 70 jlblStatus.setText(e.getMessage()); 71 } 72 finally { 73 try { 74 if (infile != null ) infile.close(); 75 } 76 catch (IOException ex) {} 77 } 78 } 79 } |
Line 54 new URL(jtfURL.getText().trim()) creates a URL for the filename entered from the text field. Line 57 url.openStream() creates an InputStream from the URL. After the input stream is established, reading data from the remote file is just like reading data locally. A BufferedReader object is created from the input stream (line 58). The text from the file is displayed in the text area (line 65).