com.zehon.ftps
Class FTPs

java.lang.Object
  extended by com.zehon.ftps.FTPs

public class FTPs
extends java.lang.Object

This FTPs class offers convenient static methods for uploading/downloading files and doing remote file options such as move, copy, etc.. This uses FTP/SSL explicitly by default. If you need the implicit option, please refer to

Author:
Zehon Team http://www.zehon.com/
See Also:
Please refer to http://www.zehon.com/features_ftps.htm for more information about our FTPs.

Method Summary
static void closeCache()
          Clean up the global cache 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.
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 ftpsFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Create a remote file on the ftp server specified by ftpsFolder/nameOfFolderToCreate If the folder already exists returns successfully
static int deleteFile(java.lang.String nameOfFileToDelete, java.lang.String ftpsFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Delete a remote file on the ftps server specified by ftpsFolder/nameOfFileToDelete
static boolean fileExists(java.lang.String ftpsFolder, java.lang.String nameOfFile, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Check if the remote ftps file exists
static boolean folderExists(java.lang.String ftpsFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Check if the remote ftpsFolder exists
static int getFile(java.lang.String remoteFileName, java.lang.String ftpsFolder, java.lang.String serverName, java.lang.String username, java.lang.String password, java.lang.String toLocalFolder)
          Retrieve a remote file specified by ftpsFolder/remoteFileName and buffer it and write a local folder called toLocalFolder
static java.io.InputStream getFileAsStream(java.lang.String remoteFileName, java.lang.String ftpsFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Retrieve a remote file specified by ftpsFolder/remoteFileName as a stream
static java.lang.String[] getFileNamesInFolder(java.lang.String ftpsFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Retrieve all filenames in a remote folder specified by ftpsFolder
static int getFolder(java.lang.String ftpsFolder, java.lang.String toLocalFolder, BatchTransferProgress batchTransferProgress, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Retrieve a remote folder specified by ftpsFolder write to a local folder called toLocalFolder
static int moveFile(java.lang.String nameOfFileToMove, java.lang.String ftpsFromFolder, java.lang.String newNameOfFile, java.lang.String ftpsToFolder, java.lang.String serverName, java.lang.String username, java.lang.String password)
          Move a remote file on the ftps server specified by ftpsFromFolder/nameOfFileToMove to another remote folder with a new name specified ftpsToFolder/newNameOfFile
static int sendFile(java.io.InputStream inputStream, java.lang.String nameOfFileToStore, java.lang.String ftpsDestFolder, 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 FTPs it to a remote FTPs Server specified by ftpsDestFolder i.e / or /ftpsRoot or /myftpsFolder, the name of the file stored is specified by nameOfFileToStore i.e "test.txt"
static int sendFile(java.lang.String localFilePath, java.lang.String ftpsDestFolder, 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 ftps it to a remote ftps folder specified by ftpsDestFolder, i.e / or /ftpsRoot or /myftpsFolder.
static int sendFile(java.lang.String localFilePath, java.lang.String ftpsDestFolder, 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 sendFolder(java.lang.String sendingFolder, java.lang.String ftpsFolder, 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 ftps folder ftpsFolder 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 ftpsDestFolder,
                           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 ftps it to a remote ftps folder specified by ftpsDestFolder, i.e / or /ftpsRoot or /myftpsFolder.

Parameters:
localFilePath - Local file path including the file name for example: C:\myfiles\test.txt or /home/joe/images/img.jpg
ftpsDestFolder - remote ftps folder, for example: / or /ftpsRoot or /myftpsFolder.
serverName - ftps server name
username - ftps username
password - ftps 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 ftpsing the file, catch the exception and log it or print out the stack trace or its cause for debug purposes.

for example:

try{
 FTPs.sendFile("C:\myfiles\test.txt", "/myftpsFolder", "ftps.myhost.com", "ftps", "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 ftpsDestFolder,
                           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 -
ftpsDestFolder -
nameOfFileToStore -
serverName -
username -
password -
Returns:
Throws:
FileTransferException

sendFile

public static int sendFile(java.io.InputStream inputStream,
                           java.lang.String nameOfFileToStore,
                           java.lang.String ftpsDestFolder,
                           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 FTPs it to a remote FTPs Server specified by ftpsDestFolder i.e / or /ftpsRoot or /myftpsFolder, 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"
ftpsDestFolder - remote ftps folder, for example: / or /ftpsRoot or /myftpsFolder.
serverName - ftps server name
username - ftps username
password - ftps password
Returns:
FileTransferStatus.SUCCESS or FileTransferStatus.INVALID_SETTINGS (if username or password or servername is not found)
Throws:
FileTransferException - If there is an error ftpsing 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\\ftpsProject\\files\\test.txt";
File localFile = new File(filePath);
try {
 is = new BufferedInputStream(new FileInputStream(localFile));
} catch (FileNotFoundException e1) {
 e1.printStackTrace();
}
String destFolder = "/myftpsFolder";
String nameOfFile = "testStream.txt";
int status = -1;
try {
 status = FTPs.sendFile(is, nameOfFile, destFolder, "ftps.myhost.com", "ftps", "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) {}}
}

See Also:
FileTransferStatus

getFileAsStream

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

Parameters:
remoteFileName - the name of the file on the remote server to retrieve, i.e test.txt
ftpsFolder - remote ftps folder, for example: / or /ftpsRoot or /myftpsFolder.
serverName - ftps server name
username - ftps username
password - ftps 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 = FTPs.getFileAsStream(nameOfFile, destFolder, "ftps.myhost.com", "ftps", "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 ftpsFolder,
                          java.lang.String serverName,
                          java.lang.String username,
                          java.lang.String password,
                          java.lang.String toLocalFolder)
                   throws FileTransferException
Retrieve a remote file specified by ftpsFolder/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
ftpsFolder - remote ftps folder, for example: / or /ftpsRoot or /myftpsFolder.
toLocalFolder - local filesystem folder to write the remote file to C:\\myfiles\\project\\ftpsProject\\writeToFolder";
serverName - ftps server name
username - ftps username
password - ftps 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 ftpsFolder = "/test";
String nameOfFile = "testStream.txt";
String toLocalFolder = "C:\\myfiles\\project\\ftpsProject\\writeToFolder";
int status = -1;
try {
status = FTPs.getFile(nameOfFile, ftpsFolder, toLocalFolder,"ftps.myhost.com", "ftps", "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 ftpsFolder,
                             java.lang.String serverName,
                             java.lang.String username,
                             java.lang.String password)
                      throws FileTransferException
Delete a remote file on the ftps server specified by ftpsFolder/nameOfFileToDelete

Parameters:
nameOfFileToDelete - the name of the file on the remote server to retrieve, i.e test.txt
ftpsFolder - remote ftps folder, for example: / or /ftpsRoot or /myftpsFolder
serverName - ftps server name
username - ftps username
password - ftps 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 ftpsFolder = "/test";
String nameOfFileToDelete = "test.txt";
int status = -1;
try {
status = FTPs.deleteFile(nameOfFileToDelete, ftpsFolder,"ftps.myhost.com", "ftps", "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 ftpsFolder,
                               java.lang.String serverName,
                               java.lang.String username,
                               java.lang.String password)
                        throws FileTransferException
Create a remote file on the ftp server specified by ftpsFolder/nameOfFolderToCreate If the folder already exists returns successfully

Parameters:
nameOfFolderToCreate - the name of the folder to be created on the remote server
ftpsFolder - remote folder, for example: / or /filetransferRoot or /myftpsFolder.
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 ftpsFolder = "/test";
String nameOfFolderToCreate = "subFolderTest";
int status = -1;
try {
status = FTPs.createFolder(nameOfFolderToCreate, ftpsFolder,"ftps.myhost.com", "ftps", "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 ftpsFromFolder,
                           java.lang.String newNameOfFile,
                           java.lang.String ftpsToFolder,
                           java.lang.String serverName,
                           java.lang.String username,
                           java.lang.String password)
                    throws FileTransferException
Move a remote file on the ftps server specified by ftpsFromFolder/nameOfFileToMove to another remote folder with a new name specified ftpsToFolder/newNameOfFile

Parameters:
nameOfFileToMove - the name of the file on the remote server to move, i.e test.txt
ftpsFromFolder - remote ftps folder, for example: / or /ftpsRoot or /myftpsFolder.
newNameOfFile - the new name of the file on the remote server to after the move, i.e test.txt
ftpsToFolder - remote ftps folder to move to, for example: / or /ftpsRoot or /myftpsFolder.
serverName - ftps server name
username - ftps username
password - ftps 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 ftpsFromFolder = "/test";
String nameOfFileToMove = "test.txt";
String ftpsToFolder = "/anotherfolder";
String newNameOfFile = "moved_test.txt";
int status = -1;
try {
FTPs.moveFile(nameOfFileToMove, ftpsFromFolder,newNameOfFile, ftpsToFolder, "ftps.myhost.com", "ftps", "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 - ftps server name
username - ftps username
password - ftps 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 = FTPs.copy(sourceFilePath, destFilePath, "ftps.myhost.com", "ftps", "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 = FTPs.copy(sourceFilePath, destFilePath, "ftps.myhost.com", "ftps", "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 ftpsFolder,
                            java.lang.String toLocalFolder,
                            BatchTransferProgress batchTransferProgress,
                            java.lang.String serverName,
                            java.lang.String username,
                            java.lang.String password)
                     throws FileTransferException
Retrieve a remote folder specified by ftpsFolder write to a local folder called toLocalFolder

Parameters:
remoteFileName - the name of the file on the remote server to retrieve, i.e test.txt
ftpsFolder - remote ftps folder, for example: / or /ftpsRoot or /myftpsFolder.
toLocalFolder - local filesystem folder to write the folder to C:\\myfiles\\project\\ftpsProject\\writeToFolder";
serverName - ftps server name
username - ftps username
password - ftps 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 ftpsFolder = "/test";
String toLocalFolder = "C:\\myfiles\\project\\ftpsProject\\writeToFolder";
int status = -1;
try {
status = FTPs.getFolder(ftpsFolder, toLocalFolder, new BatchTransferProgressDefault(), "ftps.myhost.com", "ftps", "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 ftpsFolder,
                                                      java.lang.String serverName,
                                                      java.lang.String username,
                                                      java.lang.String password)
                                               throws FileTransferException
Retrieve all filenames in a remote folder specified by ftpsFolder

Parameters:
ftpsFolder - remote ftps folder, for example: / or /ftpsRoot or /myftpsFolder.
serverName - ftps server name
username - ftps username
password - ftps 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 ftpsFolder = "/test";
int status = -1;
try {
String[] names = FTPs.getFileNamesInFolder(ftpsFolder, "ftps.myhost.com", "ftps", "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 ftpsFolder,
                             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 ftps folder ftpsFolder 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
ftpsDestFolder - remote ftps folder to upload to, for example: / or /ftpsRoot or /myftpsFolder.
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.
serverName - ftps server name
username - ftps username
password - ftps 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 ftpsing 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 = FTPs.sendFolder("C:\myfiles", "/myftpsFolder", new BatchTransferProgressDefault(), "ftps.myhost.com", "ftps", "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 ftpsFolder,
                                   java.lang.String serverName,
                                   java.lang.String username,
                                   java.lang.String password)
                            throws FileTransferException
Check if the remote ftpsFolder exists

Parameters:
ftpsFolder -
serverName - ftps server name
username - ftps username
password - ftps password
Returns:
true if exists false otherwise
Throws:
FileTransferException -

For example:
String ftpsFolder = "/test";
try {
boolean exists = FTPs.folderExists(ftpsFolder, "ftps.myhost.com", "ftps", "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 ftpsFolder,
                                 java.lang.String nameOfFile,
                                 java.lang.String serverName,
                                 java.lang.String username,
                                 java.lang.String password)
                          throws FileTransferException
Check if the remote ftps file exists

Parameters:
ftpsFolder -
nameOfFile -
serverName - ftps server name
username - ftps username
password - ftps password
Returns:
true if exists false otherwise
Throws:
FileTransferException -

For example:
String ftpsFolder = "/test";
String nameOfFile = "existFile.txt";
try {
boolean exists = FTPs.fileExists(ftpsFolder, nameOfFile, "ftps.myhost.com", "ftps", "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 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 ftpsFolder = "/test";
String nameOfFile = "testStream.txt";
String toLocalFolder = "C:\\myfiles\\writeToFolder";
int status = -1;
try {
status = FTPs.getFile(nameOfFile, ftpsFolder, toLocalFolder,"ftps.myhost.com", "ftps", "pass");

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

Throws:
FileTransferException


Copyright 2009 Zehon Team. All Rights Reserved.