Secure FTP using Java and SFTP (FTP over SSH) protocol
|
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).
- SSH server running SSHv2 protocol with sftp subsystem enabled.
- 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.
|
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 }
- Lines 14-18. Add necessary import statements.
- Lines 31-35. Constructor for SFtpExample instance. This constructor
sets the required attributes needed for establishing a secure
connection
- Line 44. Create new SshParameters instance containing information
needed to establish connection with SSH server.
- Line 47. Create new Sftp instance providing SshParameters instance
as an argument to constructor.
- 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.
- Line 53. Establish secure connection with server.
- Lines 56-57. Get directory listing from server and print
to console.
- Line 60. Disconnect from server.
- Lines 78-99. The main method for this example.
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
|