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

iTunes Gift Card Offer

For a limited time get a $50.00 iTunes gift card free with qualifying purchase.

Click for details.


News

JSCAPE Secure FTP Server 7.0 Released
08/20/2010 03:31 AM

JSCAPE launches Employee Giving Program
07/03/2010 08:26 AM

SSH Factory 3.6 Released
07/03/2010 06:22 AM

Secure iNet Factory 8.5 Released
07/03/2010 06:07 AM

Secure FTP Factory 8.5 Released
07/03/2010 06:03 AM

Secure FTP Applet 6.2 Released
06/10/2010 02:23 PM

more...


Tutorials

Enabling Phone Authentication
04/08/2009 11:24 AM

Detecting and Handling Brute Force Password Attacks
01/29/2009 09:44 AM

Creating a Domain
12/15/2008 11:33 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Formatting MimeMessages using .NET
09/14/2008 04:31 PM

Communicating with an IMAP4 server in .NET
09/14/2008 03:54 PM

more...


Articles

Open up corporate data to your partners
08/03/2010 10:01 PM

Access vital corporate documents on the go
07/01/2010 02:15 PM

SFTP and Encryption
05/17/2010 09:52 PM

Streamlining web uploads with ZIP archives
12/14/2009 10:11 AM

Using regular expressions in complex trigger conditions
09/08/2009 07:42 AM

Using custom forms to automate business processes
07/03/2009 08:51 AM

more...


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