| SSLDownload.java |
1 /*
2 * Copyright 2005 jMethods, Inc. All rights reserved.
3 * jMethods PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4 */
5
6 package com.myjavaworld.ftp.examples;
7
8 import java.io.File;
9 import java.security.KeyManagementException;
10 import java.security.NoSuchAlgorithmException;
11 import java.security.SecureRandom;
12
13 import javax.net.ssl.SSLContext;
14
15 import com.myjavaworld.ftp.ConnectionException;
16 import com.myjavaworld.ftp.ControlConnectionEvent;
17 import com.myjavaworld.ftp.ControlConnectionListener;
18 import com.myjavaworld.ftp.FTPClient;
19 import com.myjavaworld.ftp.FTPConstants;
20 import com.myjavaworld.ftp.FTPException;
21 import com.myjavaworld.ftp.ListParser;
22 import com.myjavaworld.ftp.RemoteFile;
23
24 /**
25 * This example demonstrates how to connect to an FTP site and download a file
26 * to the local system using SSL. This example assumes that the server is
27 * already set up for FTP over SSL. In order for this example to work, you must
28 * have server's certificate installed in <java_home>/lib/security/jssecacerts
29 * file or <java_home>/lib/security/cacerts file. For more information on this
30 * and to learn overriding procedures, please refer to the JSSE Reference Guilde
31 * that ships with JavaDoc 1.4.x. Or visit the URL
32 * http://java.sun.com/j2se/1.4.2/docs/guide/security/jsse/JSSERefGuide.html.
33 *
34 * @author Sai Pullabhotla, psai [at] jMethods [dot] com
35 * @version 1.0
36 */
37 public class SSLDownload {
38
39 /**
40 * @param args
41 * command line arguments.
42 * @throws InstantiationException
43 * propogated
44 * @throws ClassNotFoundException
45 * propogated
46 * @throws IllegalAccessException
47 * propogated
48 * @throws FTPException
49 * propogated
50 * @throws ConnectionException
51 * propogated
52 */
53
54 public static void main(String[] args) throws InstantiationException,
55 ClassNotFoundException, IllegalAccessException, FTPException,
56 ConnectionException {
57
58 //Determine the FTPClient implementation that you would like to use.
59 //For most FTP servers, the DefaultFTPClient should work.
60 final String className = "com.myjavaworld.ftp.DefaultFTPClient";
61 //final String className = "com.myjavaworld.ftp.AS400FTPClient";
62
63 final String hostName = "hostname_or_ipaddress";
64
65 final String user = "login_name";
66
67 final String password = "password";
68
69 //Use reflection to find the class and obtain an instance of the class.
70 FTPClient client = (FTPClient) Class.forName(className).newInstance();
71
72 //Add a ControlConnectionListener to look at the client-server
73 // conversation. This is optional.
74 client.addControlConnectionListener(new ControlConnectionListener() {
75
76 public void commandSent(ControlConnectionEvent evt) {
77 System.out.println("Command " + evt.getMessage());
78 }
79
80 public void replyReceived(ControlConnectionEvent evt) {
81 System.out.println("Reply " + evt.getMessage());
82 }
83
84 });
85
86 //Determine the directory list parser that we would like to use in this
87 //session. For most FTP servers, the default implementatiom should work
88 //fine.
89 ListParser parser = (ListParser) Class.forName(
90 "com.myjavaworld.ftp.DefaultListParser").newInstance();
91
92 //Set the list parser to use with the FTP client.
93 client.setListParser(parser);
94
95 //Configure for SSL connection.
96 client.setSSLUsage(FTPConstants.USE_EXPLICIT_SSL);
97 client.setSSLContext(createSSLContext());
98
99 //Connect to the FTP server.
100 System.out.println("Connecting...");
101 client.connect(hostName);
102 System.out.println("Connected. ");
103
104 //Login to the FTP server.
105 System.out.println("Logging in...");
106 client.login(user, password);
107 System.out.println("Logged in. ");
108
109 //Download a file
110 RemoteFile source = parser.createRemoteFile("/remote/test.txt", false);
111 File destination = new File("/local/test.txt");
112 System.out.println("Downloading: " + source);
113 client.download(source, destination, FTPConstants.TYPE_ASCII, false);
114 System.out.println("Done. ");
115
116 //Disconnect from the FTP server.
117 System.out.println("Disconnecting...");
118 client.disconnect();
119 System.out.println("Disconnected. ");
120 }
121
122 /**
123 * Creates and returns a default SSLContext.
124 *
125 * @return Default SSL Context.
126 */
127
128 private static SSLContext createSSLContext() {
129 try {
130 SSLContext context = SSLContext.getInstance("SSL");
131 context.init(null, null, new SecureRandom());
132 return context;
133 }
134 catch(KeyManagementException e) {
135 System.err.println("Failed to initialize SSLContext");
136 e.printStackTrace();
137 }
138 catch(NoSuchAlgorithmException e) {
139 System.err.println("Failed to initialize SSLContext");
140 e.printStackTrace();
141 }
142 return null;
143 }
144 }