com.zehon.sftp
Class SFTPClient

java.lang.Object
  extended by com.zehon.FileTransferClient
      extended by com.zehon.sftp.SFTPClient

public class SFTPClient
extends FileTransferClient

This SFTPClient class has methods for uploading/downloading files and doing remote file options such as move, copy, etc.. This class has getter/setter methods that can be injected via Spring. Log4j is used for logging, for debugging information please turn on the debug mode for this class package. Please refer to http://www.zehon.com/features.htm for more information about our FTP.

Author:
Zehon Team (we're happy to serve you!) http://www.zehon.com/

Constructor Summary
SFTPClient()
           
SFTPClient(java.lang.String serverName, int port, java.lang.String username, java.lang.String password)
          Constructor with serverName, username and password as parameters.
SFTPClient(java.lang.String serverName, int port, java.lang.String username, java.lang.String privateKeyPath, boolean usePrivateKey)
           
SFTPClient(java.lang.String serverName, java.lang.String username, java.lang.String password)
          Constructor with serverName, username and password, using standard port 22 as the default port as parameters.
SFTPClient(java.lang.String serverName, java.lang.String username, java.lang.String privateKeyPath, boolean usePrivateKey)
          Use this constructor when the private key is used in place of password to authenticate with the SFTP server.
 
Method Summary
 int copyFile(java.lang.String sourceFilePath, java.lang.String destFilePath)
          Copy a remote file specified by sourceFilePath to another remote folder or file path specified by destFilePath.
 int createFolder(java.lang.String nameOfFolderToCreate, java.lang.String sftpFolder)
          Create a remote file on the sftp server specified by sftpFolder/nameOfFolderToCreate If the folder already exists returns successfully
 int deleteFile(java.lang.String nameOfFileToDelete, java.lang.String sftpFolder)
          Delete a remote file on the sftp server specified by sftpFolder/nameOfFileToDelete
 boolean fileExists(java.lang.String sftpFolder, java.lang.String nameOfFile)
          Check if the remote sftp file exists
 boolean folderExists(java.lang.String sftpFolder)
          Check if the remote sftpFolder exists
 int getFile(java.lang.String remoteFileName, java.lang.String sftpFolder, java.lang.String toLocalFolder)
          Retrieve a remote file specified by sftpFolder/remoteFileName and buffer it and write a local folder called toLocalFolder
 java.io.InputStream getFileAsStream(java.lang.String remoteFileName, java.lang.String sftpFolder)
          Retrieve a remote file specified by sftpFolder/remoteFileName as a stream
 java.lang.String[] getFileNamesInFolder(java.lang.String sftpFolder)
          Retrieve all filenames in a remote folder specified by sftpFolder
protected  org.apache.commons.vfs.FileSystemOptions getFileSystemOptions()
           
protected  java.lang.String getFileTransferPath(java.lang.String hostname, java.lang.String username, java.lang.String password, int port)
           
 int getFolder(java.lang.String sftpFolder, java.lang.String toLocalFolder, BatchTransferProgress batchTransferProgress)
          Retrieve a remote folder and all the containing filess specified by sftpFolder recursively, write to a local folder called toLocalFolder sftpFolder WILL NOT BE CREATED LOCALLY BUT ONLY ITS CONTENTS AND SUBDIRECTORIES REFER TO getFolderCreate if sftpFolder needs to be created.
 int getFolder(java.lang.String sftpFolder, java.lang.String toLocalFolder, BatchTransferProgress batchTransferProgress, boolean recursive)
          Similiar to getFolder except recursive can be switched on/off
 int getFolderCreate(java.lang.String sftpFolder, java.lang.String toLocalFolder, BatchTransferProgress batchTransferProgress)
          Similiar to getFolder except it creates sftpFolder locally in toLocalFolder and retrieve all the containing filess specified by sftpFolder recursively, write to a local folder called toLocalFolder
 int getFolderCreate(java.lang.String sftpFolder, java.lang.String toLocalFolder, BatchTransferProgress batchTransferProgress, boolean recursive)
          Similiar to getFolderCreate except recursive can be switched on/off
 long getLastModificationTime(java.lang.String filePath)
          Get last modification or update time of a file or a folder
 java.util.Date getLastModificationTimeDate(java.lang.String filePath)
          Get last modification or update time of a file or a folder as a java.util.Date object
 java.lang.String getPassword()
           
 int getPort()
           
 java.lang.String getPrivateKeyPath()
           
protected  java.lang.String getProtocol()
           
 java.lang.String getServerName()
           
 java.lang.String getUsername()
           
 boolean isUsePrivateKey()
           
 int moveFile(java.lang.String nameOfFileToMove, java.lang.String sftpFromFolder, java.lang.String newNameOfFile, java.lang.String sftpToFolder)
          Move a remote file on the sftp server specified by sftpFromFolder/nameOfFileToMove to another remote folder with a new name specified sftpToFolder/newNameOfFile
 int sendFile(java.io.InputStream inputStream, java.lang.String nameOfFileToStore, java.lang.String sftpDestFolder)
          Take any input stream (all classes that implement from java.io.InputStream) and read it to a buffer and SFTP it to a remote SFTP Server specified by sftpDestFolder i.e / or /sftpRoot or /mysftpFolder, the name of the file stored is specified by nameOfFileToStore i.e "test.txt"
 int sendFile(java.lang.String localFilePath, java.lang.String sftpDestFolder)
          Read in a file specified by localFilePath, for example: C:\myfiles\test.txt or /home/joe/images/img.jpg it does not matter if the file is of binary or text, and sftp it to a remote sftp folder specified by sftpDestFolder, i.e / or /sftpRoot or /mysftpFolder.
 int sendFileOriginal(java.lang.String localFilePath, java.lang.String filetransferDestFolder)
          Read in a file specified by localFilePath, for example: C:\myfiles\test.txt or /home/joe/images/img.jpg it does not matter if the file is of binary or text, and sftp it to a remote sftp folder specified by sftpDestFolder, i.e / or /sftpRoot or /mysftpFolder.
 int sendFileOriginal(java.lang.String localFilePath, java.lang.String filetransferDestFolder, java.lang.String nameOfFileToStore)
          Similar to the other sendFileOriginal except you get to choose what name to be stored on the server
 int sendFolder(java.lang.String sendingFolder, java.lang.String sftpFolder, BatchTransferProgress batchTransferProgress)
          Transfer all files and subdirectories recursively from sendingFolder, for example: C:\myfiles or /home/joe/images to the remote sftp folder sftpFolder.
 int sendFolder(java.lang.String sendingFolder, java.lang.String sftpFolder, BatchTransferProgress batchTransferProgress, boolean recursive)
          Similiar to sendFolder, only recursive can be switched on and off The progress can be monitored by registering BatchTransferProgress object with the method
 int sendFolderCreate(java.lang.String sendingFolder, java.lang.String sftpFolder, BatchTransferProgress batchTransferProgress)
          Similiar to sendFolder, except it creates the sendingFolder remotely and sends all its content recursively to the remote sftp folder sftpFolder The progress can be monitored by registering BatchTransferProgress object with the method
 int sendFolderCreate(java.lang.String sendingFolder, java.lang.String sftpFolder, BatchTransferProgress batchTransferProgress, boolean recursive)
          Similiar to sendFolderCreate, except recursive can be switched on/off
 void setLastModificationTime(java.lang.String filePath, long modificationTime)
          Set last modification time of a file or a folder
 void setLastModificationTimeDate(java.lang.String filePath, java.util.Date modificationDate)
          Set last modification time of a file or a folder
 void setPassword(java.lang.String password)
           
 void setPort(int port)
           
 void setPrivateKeyPath(java.lang.String privateKeyPath)
           
 void setServerName(java.lang.String serverName)
           
 void setUsePrivateKey(boolean usePrivateKey)
           
 void setUsername(java.lang.String username)
           
protected  boolean validate()
          To check for private key path
 
Methods inherited from class com.zehon.FileTransferClient
closeCache, getFolder, sendFile, sendFile, sendFile, sendFile, sendFile, sendFiles, sendFolder, writeToFile
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SFTPClient

public SFTPClient()

SFTPClient

public SFTPClient(java.lang.String serverName,
                  java.lang.String username,
                  java.lang.String password)
Constructor with serverName, username and password, using standard port 22 as the default port as parameters.

Parameters:
serverName: - SFTP sername
username: - username for the SFTP server
password: - Password for the SFTP server

SFTPClient

public SFTPClient(java.lang.String serverName,
                  int port,
                  java.lang.String username,
                  java.lang.String password)
Constructor with serverName, username and password as parameters.

Parameters:
serverName: - SFTP sername
port: - SFTP port
username: - username for the SFTP server
password: - Password for the SFTP server

SFTPClient

public SFTPClient(java.lang.String serverName,
                  java.lang.String username,
                  java.lang.String privateKeyPath,
                  boolean usePrivateKey)
Use this constructor when the private key is used in place of password to authenticate with the SFTP server.

Set privateKeyPath and usePrivateKey=true to tell SFTPClient to use private key in place of password
to authenticate in order for this to work, you must have set up the paired public key in the
SFTP server under ~/.ssh/authorized_keys
For instruction on how to set this up:
Go to:http://sial.org/howto/openssh/publickey-auth/

String host = "sftp.zehon.com";
String username = "sftp";
String privateKeyPath = "C:\\myfiles\\project\\sftpServer\\keys\\id_rsa";
SFTPClient sftpClient = new SFTPClient(host, username, privateKeyPath, true );
Default Standard SFTP port: 22

Parameters:
serverName -
username -
privateKeyPath -
usePrivateKey -

SFTPClient

public SFTPClient(java.lang.String serverName,
                  int port,
                  java.lang.String username,
                  java.lang.String privateKeyPath,
                  boolean usePrivateKey)
Method Detail

getServerName

public java.lang.String getServerName()
Specified by:
getServerName in class FileTransferClient

setServerName

public void setServerName(java.lang.String serverName)
Specified by:
setServerName in class FileTransferClient

getUsername

public java.lang.String getUsername()
Specified by:
getUsername in class FileTransferClient

setUsername

public void setUsername(java.lang.String username)
Specified by:
setUsername in class FileTransferClient

getPassword

public java.lang.String getPassword()
Specified by:
getPassword in class FileTransferClient

setPassword

public void setPassword(java.lang.String password)
Specified by:
setPassword in class FileTransferClient

getPort

public int getPort()
Specified by:
getPort in class FileTransferClient

setPort

public void setPort(int port)
Specified by:
setPort in class FileTransferClient

getPrivateKeyPath

public java.lang.String getPrivateKeyPath()

setPrivateKeyPath

public void setPrivateKeyPath(java.lang.String privateKeyPath)

isUsePrivateKey

public boolean isUsePrivateKey()

setUsePrivateKey

public void setUsePrivateKey(boolean usePrivateKey)

validate

protected boolean validate()
To check for private key path

Overrides:
validate in class FileTransferClient

sendFile

public int sendFile(java.lang.String localFilePath,
                    java.lang.String sftpDestFolder)
             throws FileTransferException
Read in a file specified by localFilePath, for example: C:\myfiles\test.txt or /home/joe/images/img.jpg it does not matter if the file is of binary or text, and sftp it to a remote sftp folder specified by sftpDestFolder, i.e / or /sftpRoot or /mysftpFolder.

Overrides:
sendFile in class FileTransferClient
Parameters:
localFilePath - Local file path including the file name for example: C:\myfiles\test.txt or /home/joe/images/img.jpg
sftpDestFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error loading or sftping the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

for example:


try{
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
sftpClient.sendFile("C:\myfiles\test.txt", "/mysftpFolder");
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

sendFileOriginal

public int sendFileOriginal(java.lang.String localFilePath,
                            java.lang.String filetransferDestFolder)
                     throws FileTransferException
Read in a file specified by localFilePath, for example: C:\myfiles\test.txt or /home/joe/images/img.jpg it does not matter if the file is of binary or text, and sftp it to a remote sftp folder specified by sftpDestFolder, i.e / or /sftpRoot or /mysftpFolder. The localFilePath's modified time attribute will be retained in the sftp-ed file on the server

Overrides:
sendFileOriginal in class FileTransferClient
Parameters:
localFilePath - Local file path including the file name for example: C:\myfiles\test.txt or /home/joe/images/img.jpg
sftpDestFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error loading or sftping the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

for example:


try{
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
sftpClient.sendFileOriginal("C:\myfiles\test.txt", "/mysftpFolder");
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

sendFileOriginal

public int sendFileOriginal(java.lang.String localFilePath,
                            java.lang.String filetransferDestFolder,
                            java.lang.String nameOfFileToStore)
                     throws FileTransferException
Similar to the other sendFileOriginal except you get to choose what name to be stored on the server

Parameters:
localFilePath -
filetransferDestFolder -
nameOfFileToStore -
Returns:
Throws:
FileTransferException

sendFile

public int sendFile(java.io.InputStream inputStream,
                    java.lang.String nameOfFileToStore,
                    java.lang.String sftpDestFolder)
             throws FileTransferException
Take any input stream (all classes that implement from java.io.InputStream) and read it to a buffer and SFTP it to a remote SFTP Server specified by sftpDestFolder i.e / or /sftpRoot or /mysftpFolder, the name of the file stored is specified by nameOfFileToStore i.e "test.txt"

Overrides:
sendFile in class FileTransferClient
Parameters:
inputStream - an inputstream from a file (FileInputStream or a network input stream) anyclass that implements java.io.InputStream Note You must close the inputStream when you're done to avoid memory leak or running out of file descriptor. Look at the finally block in the example below for reference.
nameOfFileToStore - the name of the file to be stored i.e "test.txt"
sftpDestFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error sftping the stream, catch the exception and log it or print out the stack trace or its cause for debug purposes.

Example:


InputStream is = null;
String filePath = "C:\\myfiles\\project\\sftpProject\\files\\test.txt";
File localFile = new File(filePath);
try {
is = new BufferedInputStream(new FileInputStream(localFile));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String destFolder = "/mysftpFolder";
String nameOfFile = "testStream.txt";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.sendFile(is, nameOfFile, destFolder);
} catch (FileTransferException e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}finally{
if(is != null){try {is.close();} catch (IOException e) {}}
}

See Also:
FileTransferStatus

getFileAsStream

public java.io.InputStream getFileAsStream(java.lang.String remoteFileName,
                                           java.lang.String sftpFolder)
                                    throws FileTransferException
Retrieve a remote file specified by sftpFolder/remoteFileName as a stream

Overrides:
getFileAsStream in class FileTransferClient
Parameters:
remoteFileName - the name of the file on the remote server to retrieve, i.e test.txt
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
Returns:
InputStream, (null if username or password or servername is not found) Note: You must close the inputStream when you're done to avoid memory leak or running out of file descriptor. Look at the finally block in the
Throws:
FileTransferException - If there is an error getting the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


InputStream is = null;
String destFolder = "/test";
String nameOfFile = "testStream.txt";
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
is = sftpClient.getFileAsStream(nameOfFile, destFolder);
} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}finally{
if(is != null){try {is.close();} catch (IOException e) {}}
}

See Also:
FileTransferStatus

getFile

public int getFile(java.lang.String remoteFileName,
                   java.lang.String sftpFolder,
                   java.lang.String toLocalFolder)
            throws FileTransferException
Retrieve a remote file specified by sftpFolder/remoteFileName and buffer it and write a local folder called toLocalFolder

Overrides:
getFile in class FileTransferClient
Parameters:
remoteFileName - the name of the file on the remote server to retrieve, i.e test.txt
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
toLocalFolder - local filesystem folder to write the remote file to C:\\myfiles\\project\\sftpProject\\writeToFolder";
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error getting the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFolder = "/test";
String nameOfFile = "testStream.txt";
String toLocalFolder = "C:\\myfiles\\project\\sftpProject\\writeToFolder";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.getFile(nameOfFile, sftpFolder, toLocalFolder);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

getFileTransferPath

protected java.lang.String getFileTransferPath(java.lang.String hostname,
                                               java.lang.String username,
                                               java.lang.String password,
                                               int port)
Overrides:
getFileTransferPath in class FileTransferClient

deleteFile

public int deleteFile(java.lang.String nameOfFileToDelete,
                      java.lang.String sftpFolder)
               throws FileTransferException
Delete a remote file on the sftp server specified by sftpFolder/nameOfFileToDelete

Overrides:
deleteFile in class FileTransferClient
Parameters:
nameOfFileToDelete - the name of the file on the remote server to retrieve, i.e test.txt
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
Returns:
FileTransferStatus.SUCCESS FileTransferStatus.FAILURE (if failed to delete the file) FileTransferStatus.FILE_NOT_EXISTS the file does not exist on the remote server or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error deleting the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFolder = "/test";
String nameOfFileToDelete = "test.txt";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.deleteFile(nameOfFileToDelete, sftpFolder);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

createFolder

public int createFolder(java.lang.String nameOfFolderToCreate,
                        java.lang.String sftpFolder)
                 throws FileTransferException
Create a remote file on the sftp server specified by sftpFolder/nameOfFolderToCreate If the folder already exists returns successfully

Overrides:
createFolder in class FileTransferClient
Parameters:
nameOfFolderToCreate - the name of the folder to be created on the remote server
sftpFolder - remote folder, for example: / or /filetransferRoot or /mysftpFolder.
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error creating the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFolder = "/test";
String nameOfFolderToCreate = "subFolderTest";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.createFolder(nameOfFolderToCreate, sftpFolder);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

moveFile

public int moveFile(java.lang.String nameOfFileToMove,
                    java.lang.String sftpFromFolder,
                    java.lang.String newNameOfFile,
                    java.lang.String sftpToFolder)
             throws FileTransferException
Move a remote file on the sftp server specified by sftpFromFolder/nameOfFileToMove to another remote folder with a new name specified sftpToFolder/newNameOfFile

Overrides:
moveFile in class FileTransferClient
Parameters:
nameOfFileToMove - the name of the file on the remote server to move, i.e test.txt
sftpFromFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
newNameOfFile - the new name of the file on the remote server to after the move, i.e test.txt
sftpToFolder - remote sftp folder to move to, for example: / or /sftpRoot or /mysftpFolder.
Returns:
FileTransferStatus.SUCCESS FileTransferStatus.FAILURE (if failed to delete the file) FileTransferStatus.FILE_NOT_EXISTS the file does not exist on the remote server or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error moving the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFromFolder = "/test";
String nameOfFileToMove = "test.txt";
String sftpToFolder = "/anotherfolder";
String newNameOfFile = "moved_test.txt";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.moveFile(nameOfFileToMove, sftpFromFolder,newNameOfFile, sftpToFolder);
} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

copyFile

public int copyFile(java.lang.String sourceFilePath,
                    java.lang.String destFilePath)
             throws FileTransferException
Copy a remote file specified by sourceFilePath to another remote folder or file path specified by destFilePath. If sourceFilePath is a folder, all of its files will be copied to the new folder.

Overrides:
copyFile in class FileTransferClient
Parameters:
sourceFilePath - a file path with the file name or just a folder path
destFilePath - a file path with the file name or just a folder path
Returns:
FileTransferStatus.SUCCESS FileTransferStatus.FILE_NOT_EXISTS the file does not exist on the remote server or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error moving the file, catch the exception and log it or print out the stack trace or its cause for debug purposes. For example: Copying test.txt from the test folder to the upload folder with a new name call testCopy.txt
String sourceFilePath = "/test/test.txt";
String destFilePath = "/upload/testCopy.txt";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.copy(sourceFilePath, destFilePath);
} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)

}

Or:


//Copying all files in upload folder to folder uploadCopy
String sourceFilePath = "/upload";
String destFilePath = "/uploadCopy";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.copy(sourceFilePath, destFilePath);
} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

getFolder

public int getFolder(java.lang.String sftpFolder,
                     java.lang.String toLocalFolder,
                     BatchTransferProgress batchTransferProgress)
              throws FileTransferException
Retrieve a remote folder and all the containing filess specified by sftpFolder recursively, write to a local folder called toLocalFolder sftpFolder WILL NOT BE CREATED LOCALLY BUT ONLY ITS CONTENTS AND SUBDIRECTORIES REFER TO getFolderCreate if sftpFolder needs to be created.

Parameters:
remoteFileName - the name of the file on the remote server to retrieve, i.e test.txt
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
toLocalFolder - local filesystem folder to write the folder to C:\\myfiles\\project\\sftpProject\\writeToFolder";
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error getting the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFolder = "/test";
String toLocalFolder = "C:\\myfiles\\project\\sftpProject\\writeToFolder";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.getFolder(sftpFolder, toLocalFolder, new BatchTransferProgressDefault());

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

getFolder

public int getFolder(java.lang.String sftpFolder,
                     java.lang.String toLocalFolder,
                     BatchTransferProgress batchTransferProgress,
                     boolean recursive)
              throws FileTransferException
Similiar to getFolder except recursive can be switched on/off

Parameters:
remoteFileName - the name of the file on the remote server to retrieve, i.e test.txt
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
toLocalFolder - local filesystem folder to write the folder to C:\\myfiles\\project\\sftpProject\\writeToFolder";
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error getting the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFolder = "/test";
String toLocalFolder = "C:\\myfiles\\project\\sftpProject\\writeToFolder";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.getFolder(sftpFolder, toLocalFolder, new BatchTransferProgressDefault(), false);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

getFolderCreate

public int getFolderCreate(java.lang.String sftpFolder,
                           java.lang.String toLocalFolder,
                           BatchTransferProgress batchTransferProgress)
                    throws FileTransferException
Similiar to getFolder except it creates sftpFolder locally in toLocalFolder and retrieve all the containing filess specified by sftpFolder recursively, write to a local folder called toLocalFolder

Parameters:
remoteFileName - the name of the file on the remote server to retrieve, i.e test.txt
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
toLocalFolder - local filesystem folder to write the folder to C:\\myfiles\\project\\sftpProject\\writeToFolder";
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error getting the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFolder = "/test";
String toLocalFolder = "C:\\myfiles\\project\\sftpProject\\writeToFolder";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.getFolderCreate(sftpFolder, toLocalFolder, new BatchTransferProgressDefault());

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

getFolderCreate

public int getFolderCreate(java.lang.String sftpFolder,
                           java.lang.String toLocalFolder,
                           BatchTransferProgress batchTransferProgress,
                           boolean recursive)
                    throws FileTransferException
Similiar to getFolderCreate except recursive can be switched on/off

Parameters:
remoteFileName - the name of the file on the remote server to retrieve, i.e test.txt
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
toLocalFolder - local filesystem folder to write the folder to C:\\myfiles\\project\\sftpProject\\writeToFolder";
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error getting the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFolder = "/test";
String toLocalFolder = "C:\\myfiles\\project\\sftpProject\\writeToFolder";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
status = sftpClient.getFolderCreate(sftpFolder, toLocalFolder, new BatchTransferProgressDefault(), false);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

getFileNamesInFolder

public java.lang.String[] getFileNamesInFolder(java.lang.String sftpFolder)
                                        throws FileTransferException
Retrieve all filenames in a remote folder specified by sftpFolder

Overrides:
getFileNamesInFolder in class FileTransferClient
Parameters:
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
Returns:
Array of file names
Throws:
FileTransferException - If there is an error getting the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

For example:


String sftpFolder = "/test";
int status = -1;
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
String[] names = sftpClient.getFileNamesInFolder(sftpFolder);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

sendFolder

public int sendFolder(java.lang.String sendingFolder,
                      java.lang.String sftpFolder,
                      BatchTransferProgress batchTransferProgress)
               throws FileTransferException
Transfer all files and subdirectories recursively from sendingFolder, for example: C:\myfiles or /home/joe/images to the remote sftp folder sftpFolder. The progress can be monitored by registering BatchTransferProgress object with the method, set null to it if monitoring is not needed. The sendingFolder WILL NOT BE CREATED REMOTELY BUT ONLY ITS CONTENTS AND SUBDIRECTORIES REFER TO sendFolderCreate if sendingFolder needs to be created.

Parameters:
sendingFolder - Local folder to be uploaded: C:\myfiles or /home/joe/images
sftpDestFolder - remote sftp folder to upload to, for example: / or /sftpRoot or /mysftpFolder.
batchTransferProgress - an object that gets notified when a file starts or ends transfering. You can set it to null if you choose to not monitor the progress.
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error loading or sftping the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

for example:


try{
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
sftpClient.sendFolder("C:\myfiles", "/mysftpFolder", new BatchTransferProgressDefault());
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

sendFolder

public int sendFolder(java.lang.String sendingFolder,
                      java.lang.String sftpFolder,
                      BatchTransferProgress batchTransferProgress,
                      boolean recursive)
               throws FileTransferException
Similiar to sendFolder, only recursive can be switched on and off The progress can be monitored by registering BatchTransferProgress object with the method

Parameters:
sendingFolder - Local folder to be uploaded: C:\myfiles or /home/joe/images
sftpDestFolder - remote sftp folder to upload to, for example: / or /sftpRoot or /mysftpFolder.
batchTransferProgress - an object that gets notified when a file starts or ends transfering. You can set it to null if you choose to not monitor the progress.
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error loading or sftping the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

for example:


try{
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
sftpClient.sendFolder("C:\myfiles", "/mysftpFolder", new BatchTransferProgressDefault(), false);
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

sendFolderCreate

public int sendFolderCreate(java.lang.String sendingFolder,
                            java.lang.String sftpFolder,
                            BatchTransferProgress batchTransferProgress)
                     throws FileTransferException
Similiar to sendFolder, except it creates the sendingFolder remotely and sends all its content recursively to the remote sftp folder sftpFolder The progress can be monitored by registering BatchTransferProgress object with the method

Parameters:
sendingFolder - Local folder to be uploaded: C:\myfiles or /home/joe/images
sftpDestFolder - remote sftp folder to upload to, for example: / or /sftpRoot or /mysftpFolder.
batchTransferProgress - an object that gets notified when a file starts or ends transfering. You can set it to null if you choose to not monitor the progress.
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error loading or sftping the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

for example:


try{
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
sftpClient.sendFolderCreate("C:\myfiles", "/mysftpFolder", new BatchTransferProgressDefault());
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

sendFolderCreate

public int sendFolderCreate(java.lang.String sendingFolder,
                            java.lang.String sftpFolder,
                            BatchTransferProgress batchTransferProgress,
                            boolean recursive)
                     throws FileTransferException
Similiar to sendFolderCreate, except recursive can be switched on/off

Parameters:
sendingFolder - Local folder to be uploaded: C:\myfiles or /home/joe/images
sftpDestFolder - remote sftp folder to upload to, for example: / or /sftpRoot or /mysftpFolder.
batchTransferProgress - an object that gets notified when a file starts or ends transfering. You can set it to null if you choose to not monitor the progress.
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error loading or sftping the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

for example:


try{
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
sftpClient.sendFolderCreate("C:\myfiles", "/mysftpFolder", new BatchTransferProgressDefault(), false);
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

folderExists

public boolean folderExists(java.lang.String sftpFolder)
                     throws FileTransferException
Check if the remote sftpFolder exists

Overrides:
folderExists in class FileTransferClient
Parameters:
sftpFolder -
Returns:
true if exists false otherwise
Throws:
FileTransferException -

For example:
String sftpFolder = "/test";
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
boolean exists = sftpClient.folderExists(sftpFolder);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}


fileExists

public boolean fileExists(java.lang.String sftpFolder,
                          java.lang.String nameOfFile)
                   throws FileTransferException
Check if the remote sftp file exists

Overrides:
fileExists in class FileTransferClient
Parameters:
sftpFolder -
nameOfFile -
Returns:
true if exists false otherwise
Throws:
FileTransferException -

For example:
String sftpFolder = "/test";
String nameOfFile = "existFile.txt";
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
boolean exists = sftpClient.fileExists(sftpFolder, nameOfFile);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}


getLastModificationTime

public long getLastModificationTime(java.lang.String filePath)
                             throws FileTransferException
Get last modification or update time of a file or a folder

Overrides:
getLastModificationTime in class FileTransferClient
Parameters:
filePath - a file path with the file name or just a folder path
Returns:
milliseconds representing the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
Throws:
FileTransferException - If invalid settings or the file or folder does not exist or If there is an error getting last modification time

For example:
String filePath = "/test/test.txt";
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
long modTime = sftpClient.getLastModificationTime(filePath);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}


getLastModificationTimeDate

public java.util.Date getLastModificationTimeDate(java.lang.String filePath)
                                           throws FileTransferException
Get last modification or update time of a file or a folder as a java.util.Date object

Overrides:
getLastModificationTimeDate in class FileTransferClient
Parameters:
filePath - a file path with the file name or just a folder path
Returns:
java.util.Date object
Throws:
FileTransferException - If invalid settings or the file or folder does not exist or If there is an error getting last modification time

For example:
String filePath = "/test/test.txt";
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
Date modDate = sftpClient.getLastModificationTime(filePath);

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}


setLastModificationTime

public void setLastModificationTime(java.lang.String filePath,
                                    long modificationTime)
                             throws FileTransferException
Set last modification time of a file or a folder

Overrides:
setLastModificationTime in class FileTransferClient
Parameters:
filePath - a file path with the file name or just a folder path
modificationTime - long value of milliseconds representing the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
Throws:
FileTransferException - If invalid settings or the file or folder does not exist or If there is an error setting last modification time

For example:
String filePath = "/test/test.txt";
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
long curTime = System.currentTimeMillis();
sftpClient.setLastModificationTime(filePath, curTime );

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}


setLastModificationTimeDate

public void setLastModificationTimeDate(java.lang.String filePath,
                                        java.util.Date modificationDate)
                                 throws FileTransferException
Set last modification time of a file or a folder

Overrides:
setLastModificationTimeDate in class FileTransferClient
Parameters:
filePath - a file path with the file name or just a folder path
modificationDate - java.util.Date value
Throws:
FileTransferException - If invalid settings or the file or folder does not exist or If there is an error setting last modification time

For example:
String filePath = "/test/test.txt";
try {
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
sftpClient.setLastModificationDate(filePath, new Date() );

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}


getFileSystemOptions

protected org.apache.commons.vfs.FileSystemOptions getFileSystemOptions()
                                                                 throws org.apache.commons.vfs.FileSystemException
Specified by:
getFileSystemOptions in class FileTransferClient
Throws:
org.apache.commons.vfs.FileSystemException

getProtocol

protected java.lang.String getProtocol()
Specified by:
getProtocol in class FileTransferClient


Copyright 2009 Zehon Team. All Rights Reserved.