Secure FTP using Java and FTPS (FTP over SSL)
This article will demonstrate how to securely transfer files using the FTPS (FTP over SSL) protocol and the components provided in Secure FTP Factory. To see what else Secure FTP Factory has to offer Download a FREE 30 day Secure FTP Factory Evaluation.
Note: This example demonstrates using FTPS (FTP over SSL). If you are wanting to use the SFTP (FTP over SSH) protocol then please see the article Secure FTP using Java and SFTP (FTP over SSH).
- An FTP server with SSL enabled (supports both AUTH TLS and implicit SSL connections).
- JDK 1.2.2 or above and Secure FTP Factory
Note: If using JDK prior to 1.4 you may need to install the Java Secure Socket Extensions. See Installing the JSSE in the Secure FTP Factory documentation for instructions on how to do this.
Note: The example below connects using AUTH TLS over port 21. If you wish to use implicit SSL and your server supports it then set the connection type using the Ftps#setConnectionType method and change the port to the implicit SSL port (usually 990) using Ftps#setPort prior to establishing a connection.
01 /*
02 * @(#)FtpsExample.java
03 *
04 * Copyright (c) 2001-2002 JScape
05 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
06 * All rights reserved.
07 *
08 * This software is the confidential and proprietary information of
09 * JScape. ("Confidential Information"). You shall not disclose such
10 * Confidential Information and shall use it only in accordance with
11 * the terms of the license agreement you entered into with JScape.
12 */
13
14
15 import com.jscape.inet.ftp.*;
16 import com.jscape.inet.ftps.*;
17 import java.io.*;
18
19 public class FtpsExample extends FtpAdapter {
20 private String hostname;
21 private String username;
22 private String password;
23
24 /**
25 * Constructs new FtpsExample instance
26 * @param hostname the FTP hostname
27 * @param username the FTP username
28 * @param password the FTP password
29 */
30 public FtpsExample(String hostname, String username, String password) {
31 this.hostname = hostname;
32 this.username = username;
33 this.password = password;
34 }
35
36 // print out directory listing
37 public void getListing() throws FtpException {
38 Ftps ftp = new Ftps(hostname,username,password);
39
40 //capture Ftps related events
41 ftp.addFtpListener(this);
42
43 // establish connection
44 ftp.connect();
45
46 // get directory listing and print to console
47 String results = ftp.getDirListingAsString();
48 System.out.println(results);
49
50 // disconnect
51 ftp.disconnect();
52 }
53
54 // captures connect event
55 public void connected(FtpConnectedEvent evt) {
56 System.out.println("Connected to server: " + evt.getHostname());
57 }
58
59 // captures disconnect event
60 public void disconnected(FtpDisconnectedEvent evt) {
61 System.out.println("Disconnected from server: " + evt.getHostname());
62 }
63
64 /**
65 * Runs example
66 * @param args
67 */
68 public static void main(String[] args) {
69 String hostname;
70 String username;
71 String password;
72 try {
73 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
74 System.out.print("Enter FTP hostname (e.g. ftp.yourhost.com): ");
75 hostname = reader.readLine();
76 System.out.print("Enter username (e.g. anonymous): ");
77 username = reader.readLine();
78 System.out.print("Enter password (e.g. user@here.com): ");
79 password = reader.readLine();
80 FtpsExample example = new FtpsExample(hostname,username,password);
81 example.getListing();
82 }
83 catch(Exception e) {
84 e.printStackTrace();
85 }
86 }
87 }
- Lines 15-17. Add necessary import statements.
- Lines 30-34. Constructor for FtpsExample instance. This constructor sets the required attributes needed for establishing a connection
- Line 38. Create new Ftps instance providing hostname, username and password as arguments to constructor.
- Line 41. Register the FtpsExample class to capture FTP related events. In this example the FtpConnectedEvent and FtpDisconnectedEvent events are captured at lines 56 and 61respectively.
- Line 44. Establish secure connection with server.
- Lines 47-48. Get directory listing from server and print to console.
- Line 51. Disconnect from server.
- Lines 68-86. The main method for this example.
In this article you learned how to securely transfer files using the FTPS (FTP over SSL) protocol. The FTPS component in Secure FTP Factory makes this easy removing the complexities of the FTP and SSL protocols. To see what else Secure FTP Factory has to offer Download a FREE 30 day Secure FTP Factory Evaluation
|