com.zehon.sftp
Class SFTP

java.lang.Object
  extended by com.zehon.sftp.SFTP

public class SFTP
extends java.lang.Object

This SFTP class offers convenient static methods for uploading/downloading files and doing remote file options such as move, copy, etc.. Please refer to http://www.zehon.com/features.htm for more information about our SFTP.

Author:
Zehon Team http://www.zehon.com/

Method Summary
static void closeCache()
          Clean up the global cache Clean up all cache threads before your stand-alone application exits, helping it terminate the parent thread instead of hanging because the cache's thread
is still running.
static int copyFile(java.lang.String sourceFilePath, java.lang.String destFilePath, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Copy a remote file specified by sourceFilePath to another remote folder or file path specified by destFilePath.
static int createFolder(java.lang.String nameOfFolderToCreate, java.lang.String sftpFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Create a remote file on the sftp server specified by sftpFolder/nameOfFolderToCreate If the folder already exists returns successfully
static int deleteFile(java.lang.String nameOfFileToDelete, java.lang.String sftpFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Delete a remote file on the sftp server specified by sftpFolder/nameOfFileToDelete
static boolean fileExists(java.lang.String sftpFolder, java.lang.String nameOfFile, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Check if the remote sftp file exists
static boolean folderExists(java.lang.String sftpFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Check if the remote sftpFolder exists
static int getFile(java.lang.String remoteFileName, java.lang.String sftpFolder, java.lang.String serverName, java.lang.String username, java.lang.String password, java.lang.String toLocalFolder)
          Retrieve a remote file specified by sftpFolder/remoteFileName and buffer it and write a local folder called toLocalFolder
static java.io.InputStream getFileAsStream(java.lang.String remoteFileName, java.lang.String sftpFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Retrieve a remote file specified by sftpFolder/remoteFileName as a stream
static java.lang.String[] getFileNamesInFolder(java.lang.String sftpFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Retrieve all filenames in a remote folder specified by sftpFolder
static int getFolder(java.lang.String sftpFolder, java.lang.String toLocalFolder, BatchTransferProgress batchTransferProgress, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Retrieve a remote folder and all the containing files specified by sftpFolder write to a local folder called toLocalFolder
static int getFolderCreate(java.lang.String sftpFolder, java.lang.String toLocalFolder, BatchTransferProgress batchTransferProgress, java.lang.String serverName, java.lang.String username, java.lang.String password)
          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
static int moveFile(java.lang.String nameOfFileToMove, java.lang.String sftpFromFolder, java.lang.String newNameOfFile, java.lang.String sftpToFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Move a remote file on the sftp server specified by sftpFromFolder/nameOfFileToMove to another remote folder with a new name specified sftpToFolder/newNameOfFile
static int sendFile(java.io.InputStream inputStream, java.lang.String nameOfFileToStore, java.lang.String sftpDestFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          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"
static int sendFile(java.lang.String localFilePath, java.lang.String sftpDestFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          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.
static int sendFile(java.lang.String localFilePath, java.lang.String sftpDestFolder, java.lang.String nameOfFileToStore, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Similar to the other sendFile except you get to choose what name to be stored on the server
static int sendFileOriginal(java.lang.String localFilePath, java.lang.String sftpDestFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          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.
static int sendFolder(java.lang.String sendingFolder, java.lang.String sftpFolder, BatchTransferProgress batchTransferProgress, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Transfer all files or 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
static int sendFolderCreate(java.lang.String sendingFolder, java.lang.String sftpFolder, BatchTransferProgress batchTransferProgress, java.lang.String serverName, java.lang.String username, java.lang.String password)
          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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

sendFile

public static int sendFile(java.lang.String localFilePath,
                           java.lang.String sftpDestFolder,
                           java.lang.String serverName,
                           java.lang.String username,
                           java.lang.String password)
                    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.

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.
serverName - sftp server name
username - sftp username
password - sftp password
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{
SFTP.sendFile("C:\myfiles\test.txt", "/mysftpFolder", "sftp.myhost.com", "sftp", "pass" );
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

sendFile

public static int sendFile(java.lang.String localFilePath,
                           java.lang.String sftpDestFolder,
                           java.lang.String nameOfFileToStore,
                           java.lang.String serverName,
                           java.lang.String username,
                           java.lang.String password)
                    throws FileTransferException
Similar to the other sendFile except you get to choose what name to be stored on the server

Parameters:
localFilePath -
sftpDestFolder -
nameOfFileToStore -
serverName -
username -
password -
Returns:
Throws:
FileTransferException

sendFile

public static int sendFile(java.io.InputStream inputStream,
                           java.lang.String nameOfFileToStore,
                           java.lang.String sftpDestFolder,
                           java.lang.String serverName,
                           java.lang.String username,
                           java.lang.String password)
                    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"

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.
serverName - sftp server name
username - sftp username
password - sftp password
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) {
e1.printStackTrace();
}
String destFolder = "/mysftpFolder";
String nameOfFile = "testStream.txt";
int status = -1;
try {

status = SFTP.sendFile(is, nameOfFile, destFolder, "sftp.myhost.com", "sftp", "pass" );
} 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) {}}
}


sendFileOriginal

public static int sendFileOriginal(java.lang.String localFilePath,
                                   java.lang.String sftpDestFolder,
                                   java.lang.String serverName,
                                   java.lang.String username,
                                   java.lang.String password)
                            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

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{
SFTP.sendFileOriginal("C:\myfiles\test.txt", "/mysftpFolder", "sftp.myhost.com", "sftp", "pass" );
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

getFileAsStream

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

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.
serverName - sftp server name
username - sftp username
password - sftp password
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 {

is = SFTP.getFileAsStream(nameOfFile, destFolder, "sftp.myhost.com", "sftp", "pass" );
} 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 static int getFile(java.lang.String remoteFileName,
                          java.lang.String sftpFolder,
                          java.lang.String serverName,
                          java.lang.String username,
                          java.lang.String password,
                          java.lang.String toLocalFolder)
                   throws FileTransferException
Retrieve a remote file specified by sftpFolder/remoteFileName and buffer it and write 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 remote file to C:\\myfiles\\project\\sftpProject\\writeToFolder";
serverName - sftp server name
username - sftp username
password - sftp password
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 {
status = SFTP.getFile(nameOfFile, sftpFolder, toLocalFolder,"sftp.myhost.com", "sftp", "pass");

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

See Also:
FileTransferStatus

deleteFile

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

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
serverName - sftp server name
username - sftp username
password - sftp password
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 {
status = SFTP.deleteFile(nameOfFileToDelete, sftpFolder,"sftp.myhost.com", "sftp", "pass");
} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

createFolder

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

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 {
status = SFTP.createFolder(nameOfFolderToCreate, sftpFolder,"sftp.myhost.com", "sftp", "pass");

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

See Also:
FileTransferStatus

moveFile

public static int moveFile(java.lang.String nameOfFileToMove,
                           java.lang.String sftpFromFolder,
                           java.lang.String newNameOfFile,
                           java.lang.String sftpToFolder,
                           java.lang.String serverName,
                           java.lang.String username,
                           java.lang.String password)
                    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

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.
serverName - sftp server name
username - sftp username
password - sftp password
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 {
SFTP.moveFile(nameOfFileToMove, sftpFromFolder,newNameOfFile, sftpToFolder, "sftp.myhost.com", "sftp", "pass");
} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

copyFile

public static int copyFile(java.lang.String sourceFilePath,
                           java.lang.String destFilePath,
                           java.lang.String serverName,
                           java.lang.String username,
                           java.lang.String password)
                    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.

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
serverName - sftp server name
username - sftp username
password - sftp password
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 {
status = SFTP.copy(sourceFilePath, destFilePath, "sftp.myhost.com", "sftp", "pass");
} 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 {=
status = SFTP.copy(sourceFilePath, destFilePath, "sftp.myhost.com", "sftp", "pass");
} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

getFolder

public static int getFolder(java.lang.String sftpFolder,
                            java.lang.String toLocalFolder,
                            BatchTransferProgress batchTransferProgress,
                            java.lang.String serverName,
                            java.lang.String username,
                            java.lang.String password)
                     throws FileTransferException
Retrieve a remote folder and all the containing files specified by sftpFolder 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";
serverName - sftp server name
username - sftp username
password - sftp password
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 {
status = SFTP.getFolder(sftpFolder, toLocalFolder, new BatchTransferProgressDefault(),"sftp.myhost.com", "sftp", "pass");

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

See Also:
FileTransferStatus

getFolderCreate

public static int getFolderCreate(java.lang.String sftpFolder,
                                  java.lang.String toLocalFolder,
                                  BatchTransferProgress batchTransferProgress,
                                  java.lang.String serverName,
                                  java.lang.String username,
                                  java.lang.String password)
                           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";
serverName - sftp server name
username - sftp username
password - sftp password
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 {
status = SFTP.getFolderCreate(sftpFolder, toLocalFolder, new BatchTransferProgressDefault(),"sftp.myhost.com", "sftp", "pass");

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

See Also:
FileTransferStatus

getFileNamesInFolder

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

Parameters:
sftpFolder - remote sftp folder, for example: / or /sftpRoot or /mysftpFolder.
serverName - sftp server name
username - sftp username
password - sftp password
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 {
String[] names = SFTP.getFileNamesInFolder(sftpFolder,"sftp.myhost.com", "sftp", "pass");

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

See Also:
FileTransferStatus

sendFolder

public static int sendFolder(java.lang.String sendingFolder,
                             java.lang.String sftpFolder,
                             BatchTransferProgress batchTransferProgress,
                             java.lang.String serverName,
                             java.lang.String username,
                             java.lang.String password)
                      throws FileTransferException
Transfer all files or 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

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{
int status = SFTP.sendFolder("C:\myfiles", "/mysftpFolder", new BatchTransferProgressDefault(),"sftp.myhost.com", "sftp", "pass");
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

sendFolderCreate

public static int sendFolderCreate(java.lang.String sendingFolder,
                                   java.lang.String sftpFolder,
                                   BatchTransferProgress batchTransferProgress,
                                   java.lang.String serverName,
                                   java.lang.String username,
                                   java.lang.String password)
                            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{
int status = SFTP.sendFolderCreate("C:\myfiles", "/mysftpFolder", new BatchTransferProgressDefault(),"sftp.myhost.com", "sftp", "pass");
}catch(FileTransferException ex){
ex.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}

See Also:
FileTransferStatus

folderExists

public static boolean folderExists(java.lang.String sftpFolder,
                                   java.lang.String serverName,
                                   java.lang.String username,
                                   java.lang.String password)
                            throws FileTransferException
Check if the remote sftpFolder exists

Parameters:
sftpFolder -
Returns:
true if exists false otherwise
Throws:
FileTransferException -

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

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


fileExists

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

Parameters:
sftpFolder -
nameOfFile -
Returns:
true if exists false otherwise
Throws:
FileTransferException -

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

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


closeCache

public static void closeCache()
                       throws FileTransferException
Clean up the global cache Clean up all cache threads before your stand-alone application exits, helping it terminate the parent thread instead of hanging because the cache's thread
is still running. YOU DON'T HAVE TO DO THIS, it is completely OK to have the global cache thread running still,
but if it bothers you about having running threads and you don't need to do SFTP-ing any more then do this.
ONLY DO THIS BEFORE THE JVM EXITS (before the main() method exits)
DON'T USE IT IF THE APPLICATION IS RUNNING WITHIN A SERVLET CONTAINER, UNLESS YOU
CAN HOOK IT TO THE SHUTDOWN MECHANISM OF THE CONTAINER. Example:

public static void main(String[] args) {
String sftpFolder = "/test";
String nameOfFile = "testStream.txt";
String toLocalFolder = "C:\\myfiles\\writeToFolder";
int status = -1;
try {
status = SFTP.getFile(nameOfFile, sftpFolder, toLocalFolder,"sftp.myhost.com", "sftp", "pass");

} catch (Exception e) {
e.printStackTrace();
//or
//ex.getCause().printStackTrace(); (Depending on what version of JDK you use)
}
try {
SFTP.closeCache();
} catch (FileTransferException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Throws:
FileTransferException


Copyright 2009 Zehon Team. All Rights Reserved.