Secure FTP using Java - Java Secure FTP, SFTP, FTPS, FTP SSL, FTP SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

Secure FTP using Java and FTPS (FTP over SSL)

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. 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).

Prerequisites

  1. An FTP server with SSL enabled (supports both AUTH TLS and implicit SSL connections).
  2. 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.

Example

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 }
  1. Lines 15-17. Add necessary import statements.
  2. Lines 30-34. Constructor for FtpsExample instance. This constructor sets the required attributes needed for establishing a connection
  3. Line 38. Create new Ftps instance providing hostname, username and password as arguments to constructor.
  4. 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.
  5. Line 44. Establish secure connection with server.
  6. Lines 47-48. Get directory listing from server and print to console.
  7. Line 51. Disconnect from server.
  8. Lines 68-86. The main method for this example.

Summary

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