Overview
This article will demonstrate how to securely transfer files using the FTPS (FTP over SSL) protocol and the components provided in Secure FTP Factory.
Code Example
Download FtpsExample.java (2.3K)
01 /*
02 * @(#)FtpsExample.java
03 *
04 * Copyright (c) JSCAPE LLC.
05 *
06 * This software is the confidential and proprietary information of
07 * JSCAPE. ("Confidential Information"). You shall not disclose such
08 * Confidential Information and shall use it only in accordance with
09 * the terms of the license agreement you entered into with JSCAPE.
10 */
11
12 import com.jscape.inet.ftp.*;
13 import com.jscape.inet.ftps.*;
14 import java.io.*;
15 import java.util.Enumeration;
16
17 public class FtpsExample extends FtpAdapter {
18 private String hostname;
19 private String username;
20 private String password;
21
22 public FtpsExample(String hostname, String username, String password) {
23 this.hostname = hostname;
24 this.username = username;
25 this.password = password;
26 }
27
28 // print out directory listing
29 public void getListing() throws FtpException {
30 Ftps ftp = new Ftps(hostname,username,password);
31
32 //capture Ftp related events
33 ftp.addFtpListener(this);
34 ftp.connect();
35
36 String results = ftp.getDirListingAsString();
37 System.out.println(results);
38 ftp.disconnect();
39 }
40
41 // captures connect event
42 public void connected(FtpConnectedEvent evt) {
43 System.out.println("Connected to server: " + evt.getHostname());
44 }
45
46 // captures disconnect event
47 public void disconnected(FtpDisconnectedEvent evt) {
48 System.out.println("Disconnected from server: " + evt.getHostname());
49 }
50
51
52 public static void main(String[] args) {
53 String hostname;
54 String username;
55 String password;
56 try {
57 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
58 System.out.print("Enter Ftp hostname (e.g. ftp.yourhost.com): ");
59 hostname = reader.readLine();
60 System.out.print("Enter username (e.g. anonymous): ");
61 username = reader.readLine();
62 System.out.print("Enter password (e.g. user@here.com): ");
63 password = reader.readLine();
64 FtpsExample example = new FtpsExample(hostname,username,password);
65 example.getListing();
66 }
67 catch(Exception e) {
68 e.printStackTrace();
69 }
70 }
71 }
- Lines 12-15. Add necessary import statements.
- Lines 30-34. Create Ftps instance, add listener and connect.
- Lines 36-38. Get directory listing, print results and disconnect.
- Lines 42-49. Event handling methods.
Comments