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 SFTP (FTP over SSH) protocol

Overview

This article will demonstrate how to securely transfer files using the SFTP (FTP over SSH) 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 SFTP (FTP over SSH). If you are wanting to use the FTPS (FTP over SSL) protocol then please see the article Secure FTP using Java and SFTP (FTP over SSL).

Prerequisites

  1. SSH server running SSHv2 protocol with sftp subsystem enabled.
  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 Cryptography Extensions. See Installing the JCE in the Secure FTP Factory documentation for instructions on how to do this.

Example

001 /*
002  * @(#)SFtpExample.java
003  *
004  * Copyright (c) 2001-2004 JScape
005  * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
006  * All rights reserved.
007  *
008  * This software is the confidential and proprietary information of
009  * JScape. ("Confidential Information").  You shall not disclose such
010  * Confidential Information and shall use it only in accordance with
011  * the terms of the license agreement you entered into with JScape.
012  */
013 
014 import com.jscape.inet.sftp.*;
015 import com.jscape.inet.sftp.events.*;
016 import com.jscape.inet.ssh.util.SshParameters;
017 import java.io.*;
018 import java.util.Enumeration;
019 
020 public class SFtpExample extends SftpAdapter {
021     private String ftpHostname;
022     private String ftpUsername;
023     private String ftpPassword;
024     
025     /**
026      * Creates a new SFtpExample instance.
027      @param ftpHostname the FTP hostname
028      @param ftpUsername the FTP username
029      @param ftpPassword the FTP password
030      */
031     public SFtpExample(String ftpHostname, String ftpUsername, String ftpPassword) {
032         this.ftpHostname = ftpHostname;
033         this.ftpUsername = ftpUsername;
034         this.ftpPassword = ftpPassword;
035     }   
036     
037     
038     /**
039      * Prints a directory listing from FTP server
040      @throws SftpException
041      */
042     public void getListing() throws SftpException {
043       
044       SshParameters params = new SshParameters(ftpHostname,ftpUsername,ftpPassword);
045       
046       // create Sftp instance
047         Sftp ftp = new Sftp(params);
048         
049         //capture FTP related events
050         ftp.addSftpListener(this);
051         
052         // establish secure FTP connection
053         ftp.connect();
054         
055         // get directory listing
056         String results = ftp.getDirListingAsString();
057         System.out.println(results);
058         
059         // disconnect
060         ftp.disconnect();
061     }
062     
063   /**
064    * Captures FtpConnectedEvent event
065    */
066     public void connected(SftpConnectedEvent evt) {
067         System.out.println("Connected to server: " + evt.getHostname());
068     }
069     
070     /**
071      * Captures FtpDisconnectedEvent event
072      */
073     public void disconnected(SftpDisconnectedEvent evt) {
074         System.out.println("Disconnected from server: " + evt.getHostname());
075     }
076     
077     
078     public static void main(String[] args) {
079         String ftpHostname;
080         String ftpUsername;
081         String ftpPassword;
082         try {
083             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
084             System.out.print("Enter FTP hostname (e.g. ftp.myserver.com): ");
085             ftpHostname = reader.readLine();
086             System.out.print("Enter FTP username (e.g. jsmith): ");
087             ftpUsername = reader.readLine();
088             System.out.print("Enter FTP password (e.g. secret): ");
089         ftpPassword = reader.readLine();
090                     
091             SFtpExample example = new SFtpExample(ftpHostname, ftpUsername, ftpPassword);
092               
093       // print directory listing             
094             example.getListing();
095         }
096         catch(Exception e) {
097             e.printStackTrace();
098         }
099     }
100 }

  1. Lines 14-18. Add necessary import statements.
  2. Lines 31-35. Constructor for SFtpExample instance. This constructor sets the required attributes needed for establishing a secure connection
  3. Line 44. Create new SshParameters instance containing information needed to establish connection with SSH server.
  4. Line 47. Create new Sftp instance providing SshParameters instance as an argument to constructor.
  5. Line 50. Register the SFtpExample class to capture SFTP related events. In this example the SftpConnectedEvent and SftpDisconnectedEvent events are captured at lines 66 and 73 respectively.
  6. Line 53. Establish secure connection with server.
  7. Lines 56-57. Get directory listing from server and print to console.
  8. Line 60. Disconnect from server.
  9. Lines 78-99. The main method for this example.

Summary

In this article you learned how to securely transfer files using the SFTP (FTP over SSH) protocol. The SFTP component in Secure FTP Factory makes this easy removing the complexities of the SSH and SFTP protocols. To see what else Secure FTP Factory has to offer Download a FREE 30 day Secure FTP Factory Evaluation