Secure FTP Factory

com.jscape.inet.sftp
Class Sftp

java.lang.Object
  extended by com.jscape.inet.sftp.Sftp

public class Sftp
extends java.lang.Object

Implements the basic functionality of a SFTP (FTP over SSH) client.

Example usage:

 // connection information
 String hostname = "10.0.0.1";
 String username = "jsmith";
 String password = "secret";
 

// create com.jscape.inet.ssh.util.SshParameters instance SshParameters params = new SshParameters(hostname,username,password);

// create Sftp instance Sftp sftp = new Sftp(params);

// connect to SSH server sftp.connect();

// print directory listing to console System.out.println(sftp.getDirListingAsString());

// disconnect from SSH server sftp.disconnect();


Field Summary
static int ADAPTIVE_BLOCK_SIZE
          Adaptive block transfer size.
static int ASCII
           
static int AUTO
           
static int BINARY
           
 
Constructor Summary
Sftp(SshParameters params)
          Creates a new client instance.
Sftp(SshParameters params, java.io.File localDir)
          Creates a new client instance.
Sftp(SshParameters params, java.io.File localDir, SftpConfiguration config)
          Creates a new client instance.
Sftp(SshParameters params, SftpConfiguration config)
          Creates a new client instance.
 
Method Summary
 void abortDownloadThreads()
          Abort download threads.
 void abortUploadThreads()
          Abort all upload threads.
 void addSftpListener(SftpListener listener)
          Adds event listener.
static void clearProxySettings()
          Clears proxy server values.
 void connect()
          Establishes secure connection with SSH server.
 void deleteDir(java.lang.String remoteDirectory)
          Deletes directory from SSH server.
 void deleteDir(java.lang.String remoteDirectory, boolean recurse)
          Deletes directory from SSH server recursively.
 void deleteFile(java.lang.String remoteFile)
          Deletes file from SSH server.
 void disableFileAccessLogging()
          Turn off file access logging.
 void disconnect()
          Disconnects from the remote host.
 void download(java.io.OutputStream out, java.lang.String remoteFile)
          Downloads file from SSH server and writes to specified OutputStream.
 void download(java.io.OutputStream out, java.lang.String remoteFile, long off)
          Downloads file from SSH server and writes to specified OutputStream.
 java.io.File download(java.lang.String remoteFile)
          Downloads file from SSH server.
 java.io.File download(java.lang.String localFile, java.lang.String remoteFile)
          Downloads the file from SSH server.
 void downloadDir(java.lang.String remoteDirectory)
          Downloads remote directory and contents recursively from SSH server.
 void downloadDir(java.lang.String remoteDir, int retryLimit)
          Downloads remote directory and contents from SSH server.
 void downloadDir(java.lang.String remoteDir, int retryLimit, int retryInterval)
          Downloads remote directory and contents from SSH server.
 void downloadDir(java.lang.String remoteDir, int retryLimit, int retryInterval, int threadCount)
          Downloads remote directory and contents from SSH server.
 void enableFileAccessLogging()
          Turn on file access logging.
 int getBlockSize()
          Gets upload block size.
 java.io.PrintStream getDebugStream()
          Gets PrintStream used in reporting debugging statements.
 java.lang.String getDir()
          Gets current working directory of SSH server.
 java.util.Enumeration getDirListing()
          Gets directory listing from SSH server.
 java.util.Enumeration getDirListing(java.lang.String regex)
          Gets directory listing from SSH server where file names matches the regular expression.
 java.lang.String getDirListingAsString()
          Gets directory listing of files on SSH server.
 java.lang.String getDirListingAsString(java.lang.String regex)
          Gets directory listing from SSH server where file names matches the regular expression.
 int getDownloadBlockSize()
          Gets the current download buffer size.
 int getFilePermissions(java.lang.String remoteFile)
          Gets remote file permissions in UNIX chmod format (644, 777).
 long getFilesize(java.lang.String remoteFile)
          Gets filesize of remote file from SSH server.
 java.util.Date getFileTimestamp(java.lang.String remoteFile)
          Gets date/timestamp of remote file from SSH server.
 SshHostKeys getHostKeys()
          Gets SSH host keys for further use.
 java.io.InputStream getInputStream(java.lang.String filename, long offset)
          Gets an InputStream from SSH server for the purposes of downloading a file.
 java.lang.String getLineTerminator()
          Gets destination line terminator used in ASCII file transfers.
 java.io.File getLocalDir()
          Gets local directory to be used in SSH session.
 java.util.Enumeration getLocalDirListing()
          Gets local directory listing.
 java.util.Enumeration getLocalDirListing(java.lang.String regex)
          Gets local directory listing.
 int getMode()
          Gets data transfer mode for current session.
 java.util.Enumeration getNameListing()
          Gets directory listing of files on SSH server.
 java.util.Enumeration getNameListing(java.lang.String regex)
          Gets the remote directory content names.
 java.io.OutputStream getOutputStream(java.lang.String filename, long offset, boolean append)
          Gets an OutputStream from SSH server for the purposes of uploading a file.
 java.lang.String getRealPath(java.lang.String path)
          Gets real server path.
 int getRecursiveDirectoryFileCount(java.lang.String directory)
          Get size of all files rescursively in a given remote directory.
 long getRecursiveDirectorySize(java.lang.String directory)
          Get size of all files rescursively in a given remote directory.
 java.util.Vector getRemoteFileList(java.lang.String remoteDir)
          Gets a remote file list fron the current directory.
 long getTimeout()
          Gets the current connection timeout value.
 int getUploadBlockSize()
          Gets the current upload buffer size.
 void interrupt()
          Interrupts the current data transfer and sets interrupted flag to true.
 boolean interrupted()
          Checks if transfer process was interrupted.
 boolean isConnected()
          Tests whether the current service is connected to the remote host.
 boolean isDirectory(java.lang.String remotePath)
          Checks if remote pathname denotes a directory.
 boolean isValidPath(java.lang.String path)
          Checks if path is valid, i.e.
 void makeDir(java.lang.String newDirectory)
          Makes directory on SSH server.
 void makeDirRecursive(java.lang.String newDirectory)
          Makes directory on SSH server, creating subdirectories if necessary.
 java.io.File makeLocalDir(java.lang.String directoryName)
          Creates local directory.
 void mdelete(java.lang.String regex)
          Deletes multiple files matching regular expression from SSH server's current directory.
 void mdownload(java.util.Enumeration fileNames)
          Downloads specified files in current working remote directory from SSH server to local directory.
 void mdownload(java.lang.String regex)
          Downloads multiple files matching filter from SSH server's current directory.
 void mupload(java.util.Enumeration fileNames)
          Uploads multiple files to SSH server.
 void mupload(java.lang.String regex)
          Uploads multiple files matching filter to SSH server's current directory.
 void removeSftpListener(SftpListener listener)
          Removes specified SSH event listener.
 void renameFile(java.lang.String remoteFile, java.lang.String newFile)
          Renames remote file on SSH server.
 void reset()
          Resets the interrupted flag to false.
 java.io.File resumeDownload(java.lang.String remoteFile, long off)
          Resumes download of file from SSH server.
 java.io.File resumeDownload(java.lang.String localFile, java.lang.String remoteFile, long off)
          Resumes download of file from SSH server.
 void resumeUpload(java.io.File localFile, long off)
          Resumes upload of file to SSH server.
 void resumeUpload(java.io.File localFile, java.lang.String remoteFile, long off)
          Resumes upload of file to SSH server.
 void resumeUpload(java.lang.String localFile, long off)
          Resumes upload of file to SSH server.
 void resumeUpload(java.lang.String localFile, java.lang.String remoteFile, long off)
          Resumes upload of file to SSH server.
 void setAscii()
          Sets data transfer mode to ASCII.
 void setAuto(boolean auto)
          Sets data transfer mode to automatic detect Any files with a base Content-Type value of text will be transferred using ASCII, all other files will be transfered using binary.
 void setBinary()
          Sets data transfer mode to binary.
 void setBlockSize(int blockSize)
          Deprecated. see #setUploadBlockSize
 void setDebug(boolean debug)
          Sets debugging state.
 void setDebugStream(java.io.PrintStream debugStream)
          Sets PrintStream used in reporting debugging statements.
 void setDir(java.lang.String remoteDirectory)
          Sets current directory on SSH server.
 void setDirUp()
          Sets current directory on SSH server to parent directory.
 void setDownloadBlockSize(int blockSize)
          Sets the transfer buffer size for download operation.
 void setFilePermissions(java.lang.String remoteFile, int permissions)
          Sets file permissions.
 void setLineTerminator(java.lang.String lineTerminator)
          Sets destination line terminator used in ASCII file transfers.
 void setLocalDir(java.io.File localDirectory)
          Sets local directory to be used in session.
 void setProxyAuthentication(java.lang.String proxyUsername, java.lang.String proxyPassword)
          Sets the proxy authentication username and password to use with proxy server.
 void setProxyHost(java.lang.String proxyHostname, int proxyPort)
          Sets the proxy hostname and port for this connection.
 void setProxyType(java.lang.String proxyType)
          Sets the proxy type will be used for this FTP connection.
 void setReceiveBufferSize(int receiveBufferSize)
          Sets the receive buffer size option of the sftp connection.
 void setSendBufferSize(int sendBufferSize)
          Sets the send buffer size option of the sftp connection.
 void setTimeout(long timeout)
          Sets the timeout for opening connection to SSH server.
 void setUploadBlockSize(int blockSize)
          Sets the transfer buffer size for upload operations.
 void upload(byte[] data, java.lang.String remoteFile)
          Uploads file to SSH server where contents of file is not on disk but in memory.
 void upload(byte[] data, java.lang.String remoteFile, boolean append)
          Uploads file to SSH server where contents of file is not on disk but in memory.
 void upload(java.io.File localFile)
          Uploads file to SSH server.
 void upload(java.io.File localFile, boolean append)
          Uploads file to SSH server.
 void upload(java.io.File localFile, java.lang.String remoteFile)
          Uploads file to SSH server.
 void upload(java.io.File localFile, java.lang.String remoteFile, boolean append)
          Uploads file to SSH server.
 void upload(java.io.InputStream input, java.lang.String remoteFile)
          Uploads file to SSH server.
 void upload(java.io.InputStream input, java.lang.String remoteFile, boolean append)
          Uploads file to SSH server.
 void upload(java.lang.String localFile)
          Uploads file to SSH server.
 void upload(java.lang.String localFile, boolean append)
          Uploads file to SSH server.
 void upload(java.lang.String extension, java.io.File localFile)
          Uploads a file to SSH server.
 void upload(java.lang.String localFile, java.lang.String remoteFile)
          Uploads file to SSH server.
 void upload(java.lang.String localFile, java.lang.String remoteFile, boolean append)
          Uploads file to SSH server.
 void uploadDir(java.io.File localDirectory)
          Uploads local directory and contents recursively to SSH server.
 void uploadDir(java.io.File localDir, int retryLimit)
          Uploads local directory and contents.
 void uploadDir(java.io.File localDirectory, int retryLimit, int threadCount)
          Uploads local directory and contents recursively to SSH server.
 void uploadDir(java.io.File localDir, int retryLimit, int retryInterval, java.lang.String extension)
          Uploads local directory and contents.
 void uploadDir(java.io.File localDir, int retryLimit, int retryInterval, java.lang.String extension, int threadCount)
          Uploads local directory and contents.
 void uploadDir(java.io.File localDir, int retryLimit, java.lang.String extension)
          Uploads local directory and contents.
 void uploadDir(java.io.File localDir, int retryLimit, java.lang.String extension, int threadCount)
          Uploads local directory and contents.
 void uploadDir(java.io.File localDirectory, java.lang.String remoteDirName, java.lang.String extension)
          Uploads local directory and contents recursively to SSH server.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AUTO

public static final int AUTO
See Also:
Constant Field Values

ASCII

public static final int ASCII
See Also:
Constant Field Values

BINARY

public static final int BINARY
See Also:
Constant Field Values

ADAPTIVE_BLOCK_SIZE

public static final int ADAPTIVE_BLOCK_SIZE
Adaptive block transfer size.

See Also:
Constant Field Values
Constructor Detail

Sftp

public Sftp(SshParameters params)
Creates a new client instance.

Parameters:
params - the connection parameters

Sftp

public Sftp(SshParameters params,
            SftpConfiguration config)
Creates a new client instance.

Parameters:
params - the connection parameters
config - the connection configuration

Sftp

public Sftp(SshParameters params,
            java.io.File localDir)
Creates a new client instance.

Parameters:
params - the connection parameters
localDir - the local working directory

Sftp

public Sftp(SshParameters params,
            java.io.File localDir,
            SftpConfiguration config)
Creates a new client instance.

Parameters:
params - the connection parameters
localDir - the local working directory
config - the connection configuration
Method Detail

clearProxySettings

public static void clearProxySettings()
Clears proxy server values.


addSftpListener

public void addSftpListener(SftpListener listener)
Adds event listener.

Parameters:
listener - the listener to add

removeSftpListener

public void removeSftpListener(SftpListener listener)
Removes specified SSH event listener.

Parameters:
listener - the listener to remove

getTimeout

public long getTimeout()
Gets the current connection timeout value.

Returns:
the timeout in milliseconds

setTimeout

public void setTimeout(long timeout)
Sets the timeout for opening connection to SSH server.

Parameters:
timeout - the timeout in milliseconds

getMode

public int getMode()
Gets data transfer mode for current session. Default mode is AUTO where 0 = AUTO, 1 = ASCII, 2 = BINARY

Returns:
the data transfer mode

setAscii

public void setAscii()
Sets data transfer mode to ASCII. You must be connected to the SSH server prior to invoking this method.


setBinary

public void setBinary()
Sets data transfer mode to binary. You must be connected to the SSH server prior to invoking this method.


setAuto

public void setAuto(boolean auto)
Sets data transfer mode to automatic detect Any files with a base Content-Type value of text will be transferred using ASCII, all other files will be transfered using binary.

Example: file.txt, Content-Type = text/plain, uploaded using ASCII
Example: image.jpg Content-Type = image/jpg, uploaded using binary

Parameters:
auto - true to turn automatic detect on false to turn off

setBlockSize

public void setBlockSize(int blockSize)
Deprecated. see #setUploadBlockSize

Sets upload block size.

Parameters:
blockSize - the size in bytes

getBlockSize

public int getBlockSize()
Gets upload block size.

Returns:
the upload block size in bytes

setUploadBlockSize

public void setUploadBlockSize(int blockSize)
Sets the transfer buffer size for upload operations.

Parameters:
blockSize - desired buffer size

getUploadBlockSize

public int getUploadBlockSize()
Gets the current upload buffer size.

Returns:
the current upload buffer size

setDownloadBlockSize

public void setDownloadBlockSize(int blockSize)
Sets the transfer buffer size for download operation.

Parameters:
blockSize - desired buffer size

getDownloadBlockSize

public int getDownloadBlockSize()
Gets the current download buffer size.

Returns:
the current download buffer size

setLineTerminator

public void setLineTerminator(java.lang.String lineTerminator)
Sets destination line terminator used in ASCII file transfers. Default is null. If null, SFTP server must support ASCII transfers and it will be the responsibility of server to perform conversion. Otherwise, client will perform conversion from local system line terminator to specified destination line terminator during transfer.

Parameters:
lineTerminator - line terminator

getLineTerminator

public java.lang.String getLineTerminator()
Gets destination line terminator used in ASCII file transfers. Default is null. If null, SFTP server must support ASCII transfers and it will be the responsibility of server to perform conversion. Otherwise, client will perform conversion from local system line terminator to specified destination line terminator during transfer.

Returns:
current line terminator

setProxyAuthentication

public void setProxyAuthentication(java.lang.String proxyUsername,
                                   java.lang.String proxyPassword)
Sets the proxy authentication username and password to use with proxy server. To clear these settings invoke the #clearProxySettings method.

Parameters:
proxyUsername - the proxy username
proxyPassword - the proxy password
See Also:
clearProxySettings()

setProxyHost

public void setProxyHost(java.lang.String proxyHostname,
                         int proxyPort)
Sets the proxy hostname and port for this connection. To clear these settings invoke the #clearProxySettings method.

Parameters:
proxyHostname - the hostname or ip address of the proxy server
proxyPort - the port of the proxy server to connect to
See Also:
clearProxySettings()

setProxyType

public void setProxyType(java.lang.String proxyType)
Sets the proxy type will be used for this FTP connection.

Parameters:
proxyType - The proxy type. Values allowed: HTTP, SOCKS5

disableFileAccessLogging

public void disableFileAccessLogging()
Turn off file access logging. This is different from Sftp.setDebug(false), file access subsystem has it's own file logging facility, this method maybe needed to prevent file logging stack-traces & exceptions from being printed to the console. Such an exception may arise if the connection to the remote server is lost during an operation. E.g the server drops the connection during a file upload.


enableFileAccessLogging

public void enableFileAccessLogging()
Turn on file access logging. This is different from Sftp.setDebug(true), file access subsystem has it's own file logging facility, this method maybe needed to enable file logging stack-traces & exceptions and have them printed to the console. Such an exception may arise if the connection to the remote server is lost during an operation. E.g the server drops the connection during a file upload.


setDebug

public void setDebug(boolean debug)
Sets debugging state. All debug statements are reported to debug print stream. Default debug print stream is System.out.

Parameters:
debug - true to turn debugging on
See Also:
setDebugStream(java.io.PrintStream)

setDebugStream

public void setDebugStream(java.io.PrintStream debugStream)
Sets PrintStream used in reporting debugging statements. Default PrintStream is System.out.

Parameters:
debugStream - the PrintStream to send debug statements to
See Also:
PrintStream

getDebugStream

public java.io.PrintStream getDebugStream()
Gets PrintStream used in reporting debugging statements. Default PrintStream is System.out.

Returns:
the PrintStream to send debug statements to
See Also:
PrintStream

isConnected

public boolean isConnected()
Tests whether the current service is connected to the remote host.

Returns:
true if the service is connected; false otherwise

connect

public void connect()
             throws SftpException
Establishes secure connection with SSH server.

Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
SshParameters

disconnect

public void disconnect()
Disconnects from the remote host.


getDir

public java.lang.String getDir()
Gets current working directory of SSH server.

Returns:
the current working directory

setDir

public void setDir(java.lang.String remoteDirectory)
            throws SftpException
Sets current directory on SSH server.

Parameters:
remoteDirectory - the remote directory
Throws:
SftpException - if an I/O or SSH error occurs.

setDirUp

public void setDirUp()
              throws SftpException
Sets current directory on SSH server to parent directory.

Throws:
SftpException - If an error occurs

getLocalDir

public java.io.File getLocalDir()
Gets local directory to be used in SSH session. Downloads will be stored relative to local directory. Uploads are performed relative to current local directory.

Returns:
the local directory

setLocalDir

public void setLocalDir(java.io.File localDirectory)
Sets local directory to be used in session. Downloads will be stored relative to local directory. Uploads are performed relative to current local directory.

Parameters:
localDirectory - the local directory

getDirListing

public java.util.Enumeration getDirListing()
                                    throws SftpException
Gets directory listing from SSH server.

Returns:
an Enumeration of SftpFile objects
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
SftpFile, getDirListingAsString()

getDirListing

public java.util.Enumeration getDirListing(java.lang.String regex)
                                    throws SftpException
Gets directory listing from SSH server where file names matches the regular expression.

For example, to list all files with extension .java or .class the regex value should be ".+\\.(?:java|class)".

Parameters:
regex - Regular expression used to obtain the file list
Returns:
file list which matches the regular expression.
Throws:
SftpException - If an error occurs.

getDirListingAsString

public java.lang.String getDirListingAsString()
                                       throws SftpException
Gets directory listing of files on SSH server.

Returns:
a directory listing
Throws:
SftpException - if an I/O or SSH error occurs.

getDirListingAsString

public java.lang.String getDirListingAsString(java.lang.String regex)
                                       throws SftpException
Gets directory listing from SSH server where file names matches the regular expression.

Parameters:
regex - regular expression used to obtain the file list
Returns:
a directory listing string
Throws:
SftpException - if an I/O or SSH error occurs.

getNameListing

public java.util.Enumeration getNameListing()
                                     throws SftpException
Gets directory listing of files on SSH server.

Returns:
an Enumeration of String where each item represents a file or directory name
Throws:
SftpException - if an I/O or SSH error occurs.

getNameListing

public java.util.Enumeration getNameListing(java.lang.String regex)
                                     throws SftpException
Gets the remote directory content names.

Parameters:
regex - regular expression used to obtain the file list
Returns:
an Enumeration of String containing filenames
Throws:
SftpException - if an I/O or SSH error occurs.

getLocalDirListing

public java.util.Enumeration getLocalDirListing(java.lang.String regex)
Gets local directory listing.

Parameters:
regex - Regular expression used to obtain the file list
Returns:
an Enumeration of File

getLocalDirListing

public java.util.Enumeration getLocalDirListing()
Gets local directory listing.

Returns:
an Enumeration of File

makeDir

public void makeDir(java.lang.String newDirectory)
             throws SftpException
Makes directory on SSH server.

Parameters:
newDirectory - name of directory to make
Throws:
SftpException - if an I/O or SSH error occurs.

makeDirRecursive

public void makeDirRecursive(java.lang.String newDirectory)
                      throws SftpException
Makes directory on SSH server, creating subdirectories if necessary.

Parameters:
newDirectory - full path of directory to make
Throws:
SftpException - if an I/O or SSH error occurs

makeLocalDir

public java.io.File makeLocalDir(java.lang.String directoryName)
Creates local directory. Directory is relative to current local directory

Parameters:
directoryName - name of directory
Returns:
the created directory
See Also:
getLocalDir()

deleteDir

public void deleteDir(java.lang.String remoteDirectory)
               throws SftpException
Deletes directory from SSH server.

Parameters:
remoteDirectory - the directory to delete
Throws:
SftpException - if an I/O or SSH error occurs.

deleteDir

public void deleteDir(java.lang.String remoteDirectory,
                      boolean recurse)
               throws SftpException
Deletes directory from SSH server recursively.

Parameters:
remoteDirectory - the directory to delete.
recurse - true if directory contents should be deleted recursively
Throws:
SftpException - if an I/O or SSH error occurs.

isDirectory

public boolean isDirectory(java.lang.String remotePath)
                    throws SftpException
Checks if remote pathname denotes a directory.

Parameters:
remotePath - remote pathname
Returns:
true if remote pathname denotes a directory; false otherwise
Throws:
SftpException - if an I/O or SSH error occurs.

getFilePermissions

public int getFilePermissions(java.lang.String remoteFile)
                       throws SftpException
Gets remote file permissions in UNIX chmod format (644, 777).

Parameters:
remoteFile - the remote filename
Returns:
remote file permissions in UNIX chmod format
Throws:
SftpException - if an I/O or SSH error occurs.

setFilePermissions

public void setFilePermissions(java.lang.String remoteFile,
                               int permissions)
                        throws SftpException
Sets file permissions.

Parameters:
remoteFile - remote file name
permissions - UNIX chmod file permissions
Throws:
SftpException - if an I/O or SSH error occurs.

setSendBufferSize

public void setSendBufferSize(int sendBufferSize)
Sets the send buffer size option of the sftp connection.

Parameters:
sendBufferSize - Send buffer size.

setReceiveBufferSize

public void setReceiveBufferSize(int receiveBufferSize)
Sets the receive buffer size option of the sftp connection.

Parameters:
receiveBufferSize - Receive buffer size.

getFileTimestamp

public java.util.Date getFileTimestamp(java.lang.String remoteFile)
                                throws SftpException
Gets date/timestamp of remote file from SSH server.

Parameters:
remoteFile - the remote filename
Returns:
the date/timestamp of remote file
Throws:
SftpException - if an I/O or SSH error occurs.

getFilesize

public long getFilesize(java.lang.String remoteFile)
                 throws SftpException
Gets filesize of remote file from SSH server.

Parameters:
remoteFile - the remote file
Returns:
filesize in bytes
Throws:
SftpException - if an I/O or SSH error occurs.

getInputStream

public java.io.InputStream getInputStream(java.lang.String filename,
                                          long offset)
                                   throws SftpException
Gets an InputStream from SSH server for the purposes of downloading a file.

Parameters:
filename - remote file name to download
offset - file offset in bytes
Returns:
an InputStream
Throws:
SftpException - if an I/O or SSH error occurs.

getOutputStream

public java.io.OutputStream getOutputStream(java.lang.String filename,
                                            long offset,
                                            boolean append)
                                     throws SftpException
Gets an OutputStream from SSH server for the purposes of uploading a file.

Parameters:
filename - the remote filename to store file as
offset - file offset in bytes
append - true to append to remote file on SSH server, false otherwise
Returns:
an OutputStream
Throws:
SftpException - if an I/O or SSH error occurs.

deleteFile

public void deleteFile(java.lang.String remoteFile)
                throws SftpException
Deletes file from SSH server.

Parameters:
remoteFile - the file to delete
Throws:
SftpException - if an I/O or SSH error occurs.

mdelete

public void mdelete(java.lang.String regex)
             throws SftpException
Deletes multiple files matching regular expression from SSH server's current directory. Regular expressions may be used to specify multiple files e.g. ".*\\.txt" will delete all files ending in .txt extension.

Parameters:
regex - the regular expression to use
Throws:
SftpException - if an I/O or SSH error occurs.

renameFile

public void renameFile(java.lang.String remoteFile,
                       java.lang.String newFile)
                throws SftpException
Renames remote file on SSH server.

Parameters:
remoteFile - name of file to rename
newFile - new name of file
Throws:
SftpException - if an I/O or SSH error occurs.

getRealPath

public java.lang.String getRealPath(java.lang.String path)
                             throws SftpException
Gets real server path.

Parameters:
path - path to resolve
Returns:
real server path
Throws:
SftpException - if an error occcurs

isValidPath

public boolean isValidPath(java.lang.String path)
                    throws SftpException
Checks if path is valid, i.e. denotes existing file or directory.

Parameters:
path - path to check
Returns:
true if path is valid; false otherwise
Throws:
SftpException - if an error occurs

interrupt

public void interrupt()
Interrupts the current data transfer and sets interrupted flag to true. After invoking this method you must reset the interrupted flag to false before transferring any files.

See Also:
reset()

interrupted

public boolean interrupted()
Checks if transfer process was interrupted.

Returns:
true if transfer process was interrupted; false otherwise

reset

public void reset()
Resets the interrupted flag to false. This method should be invoked if you have interrupted a transfer using the Sftp#interrupt method and wish to resume transfering files.

See Also:
interrupt()

download

public java.io.File download(java.lang.String remoteFile)
                      throws SftpException
Downloads file from SSH server.

Parameters:
remoteFile - the remote file to download
Returns:
the file downloaded
Throws:
SftpException - if an I/O or SSH error occurs.

download

public java.io.File download(java.lang.String localFile,
                             java.lang.String remoteFile)
                      throws SftpException
Downloads the file from SSH server.

Parameters:
localFile - the local file to download
remoteFile - the remote file to download
Returns:
the file downloaded
Throws:
SftpException - if an I/O or SSH error occurs.

download

public void download(java.io.OutputStream out,
                     java.lang.String remoteFile)
              throws SftpException
Downloads file from SSH server and writes to specified OutputStream.

Parameters:
out - the OutputStream to write file to
remoteFile - the remote file to download
Throws:
SftpException - if an I/O or SSH error occurs.

download

public void download(java.io.OutputStream out,
                     java.lang.String remoteFile,
                     long off)
              throws SftpException
Downloads file from SSH server and writes to specified OutputStream.

Parameters:
out - the OutputStream to write file to
remoteFile - the remote file to download
off - the offset into the remote file in bytes
Throws:
SftpException - if an I/O or SSH error occurs.

resumeDownload

public java.io.File resumeDownload(java.lang.String remoteFile,
                                   long off)
                            throws SftpException
Resumes download of file from SSH server. Downloads are stored relative to current local directory.

Parameters:
remoteFile - the file to download
off - file offset in bytes
Returns:
the file downloaded
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(File)

resumeDownload

public java.io.File resumeDownload(java.lang.String localFile,
                                   java.lang.String remoteFile,
                                   long off)
                            throws SftpException
Resumes download of file from SSH server. Downloads are stored relative to current local directory.

Parameters:
localFile - local file name
remoteFile - the file to download
off - file offset in bytes
Returns:
file downloaded
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(File)

downloadDir

public void downloadDir(java.lang.String remoteDirectory)
                 throws SftpException
Downloads remote directory and contents recursively from SSH server. Downloads are stored relative to current local directory.

Parameters:
remoteDirectory - the directory to download
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(File)

downloadDir

public void downloadDir(java.lang.String remoteDir,
                        int retryLimit)
                 throws SftpException
Downloads remote directory and contents from SSH server. Downloads are stored relative to current local directory.

Parameters:
remoteDir - the directory to download
retryLimit - The retry limit number
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(java.io.File)

downloadDir

public void downloadDir(java.lang.String remoteDir,
                        int retryLimit,
                        int retryInterval,
                        int threadCount)
                 throws SftpException
Downloads remote directory and contents from SSH server. Downloads are stored relative to current local directory.

Parameters:
remoteDir - the directory to download
retryLimit - The retry limit number
retryInterval - the retry interval in seconds
threadCount - the number of threads.
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(java.io.File)

abortDownloadThreads

public void abortDownloadThreads()
Abort download threads.


downloadDir

public void downloadDir(java.lang.String remoteDir,
                        int retryLimit,
                        int retryInterval)
                 throws SftpException
Downloads remote directory and contents from SSH server. Downloads are stored relative to current local directory.

Parameters:
remoteDir - the directory to download
retryLimit - The retry limit number
retryInterval - the retry interval in seconds
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(java.io.File)

mdownload

public void mdownload(java.util.Enumeration fileNames)
               throws SftpException
Downloads specified files in current working remote directory from SSH server to local directory. Do not use this method to download a directory, see downloadDir(String) method. Downloads are stored relative to current local directory.

Parameters:
fileNames - filenames of files to download
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
downloadDir(String), setLocalDir(File)

mdownload

public void mdownload(java.lang.String regex)
               throws SftpException
Downloads multiple files matching filter from SSH server's current directory. Downloads are stored relative to current local directory. Valid filters may indicate that a file name ends with a or starts with a given string.

For example to download all files ending with the extension ".txt" use the filter ".*\\.txt". To download all files in the remote directory use the filter ".+". To download all files that start with the string "file" use the filter "file.*".

Only files matching the filter criteria in current remote directory will be considered. To download a directory use the downloadDir method.

Parameters:
regex - the filter to use
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
downloadDir(java.lang.String), setLocalDir(File)

upload

public void upload(byte[] data,
                   java.lang.String remoteFile)
            throws SftpException
Uploads file to SSH server where contents of file is not on disk but in memory.

Parameters:
data - the raw contents of the file to upload
remoteFile - the filename to store file as on SSH server
Throws:
SftpException - if an I/O or SSH error occurs.

upload

public void upload(java.io.InputStream input,
                   java.lang.String remoteFile,
                   boolean append)
            throws SftpException
Uploads file to SSH server. Destination filename should NOT contain any path information. Destination file will be stored relative to current remote directory.

Parameters:
input - the input stream to read from
remoteFile - the filename to store file as on SSH server
append - true to append to remote file, false otherwise
Throws:
SftpException - if an I/O or SSH error occurs

upload

public void upload(java.io.InputStream input,
                   java.lang.String remoteFile)
            throws SftpException
Uploads file to SSH server. Destination filename should NOT contain any path information. Destination file will be stored relative to current remote directory.

Parameters:
input - the input stream to read from
remoteFile - the filename to store file as on SSH server
Throws:
SftpException - if an I/O or SSH error occurs

upload

public void upload(byte[] data,
                   java.lang.String remoteFile,
                   boolean append)
            throws SftpException
Uploads file to SSH server where contents of file is not on disk but in memory.

Parameters:
data - the raw contents of the file to upload
remoteFile - the filename to store file as on SSH server
append - true to appending to remote file on SSH server; false otherwise
Throws:
SftpException - if an I/O or SSH error occurs.

upload

public void upload(java.io.File localFile)
            throws SftpException
Uploads file to SSH server. If localFile is a directory then directory and entire contents are uploaded.

Parameters:
localFile - the local file to upload
Throws:
SftpException - if an I/O or SSH error occurs.

upload

public void upload(java.lang.String extension,
                   java.io.File localFile)
            throws SftpException
Uploads a file to SSH server. File name will be uploaded with the extension specified by the argument and after the file has been uploaded, the file is renamed.

Parameters:
extension - The extension will be used
localFile - The local file to upload
Throws:
SftpException - if an I/O or SSH related error occurs

upload

public void upload(java.io.File localFile,
                   boolean append)
            throws SftpException
Uploads file to SSH server.

Parameters:
localFile - the local file to upload
append - true to appending to remote file on SSH server, false otherwise
Throws:
SftpException - if an I/O or SSH error occurs.

upload

public void upload(java.io.File localFile,
                   java.lang.String remoteFile)
            throws SftpException
Uploads file to SSH server. If localFile is a directory then directory and entire contents are uploaded.

Parameters:
localFile - the local file to upload
remoteFile - the destination of upload
Throws:
SftpException - if an I/O or SSH error occurs.

upload

public void upload(java.io.File localFile,
                   java.lang.String remoteFile,
                   boolean append)
            throws SftpException
Uploads file to SSH server. Uploads are performed relative to current local directory.

Parameters:
localFile - the local file to upload
remoteFile - the destination of upload
append - true to appending to remote file on SSH server, false otherwise
Throws:
SftpException - if an I/O or SSH error occurs.

upload

public void upload(java.lang.String localFile)
            throws SftpException
Uploads file to SSH server. Uploads are performed relative to current local directory.

Parameters:
localFile - the local file to upload
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(File)

upload

public void upload(java.lang.String localFile,
                   boolean append)
            throws SftpException
Uploads file to SSH server. Uploads are performed relative to current local directory.

Parameters:
localFile - the local file to upload
append - true to appending to remote file on SSH server, false otherwise
Throws:
SftpException - if an I/O or SSH error occurs.

upload

public void upload(java.lang.String localFile,
                   java.lang.String remoteFile)
            throws SftpException
Uploads file to SSH server. Uploads are performed relative to current local directory. Destination filename should not contain any path information. Destination file will be stored relative to current remote directory.

Parameters:
localFile - the local file to upload
remoteFile - the destination filename of upload
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(File)

upload

public void upload(java.lang.String localFile,
                   java.lang.String remoteFile,
                   boolean append)
            throws SftpException
Uploads file to SSH server. Uploads are performed relative to current local directory. Destination filename should not contain any path information. Destination file will be stored relative to current remote directory.

Parameters:
localFile - the local file to upload
remoteFile - the destination filename of upload
append - true to appending to remote file on SSH server, false otherwise
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(File)

resumeUpload

public void resumeUpload(java.io.File localFile,
                         long off)
                  throws SftpException
Resumes upload of file to SSH server.

Parameters:
localFile - the local file to upload
off - file offset in bytes
Throws:
SftpException - if an I/O or SSH error occurs.

resumeUpload

public void resumeUpload(java.io.File localFile,
                         java.lang.String remoteFile,
                         long off)
                  throws SftpException
Resumes upload of file to SSH server.

Parameters:
localFile - the local file to upload
remoteFile - the destination remote file
off - the local file offset in bytes
Throws:
SftpException - if an I/O or SSH related error occurs

resumeUpload

public void resumeUpload(java.lang.String localFile,
                         long off)
                  throws SftpException
Resumes upload of file to SSH server. Uploads are relative to current local directory.

Parameters:
localFile - the local file to upload
off - file offset in bytes
Throws:
SftpException - if an I/O or SSH error occurs.
See Also:
setLocalDir(File)

resumeUpload

public void resumeUpload(java.lang.String localFile,
                         java.lang.String remoteFile,
                         long off)
                  throws SftpException
Resumes upload of file to SSH server. Uploads are relative to current local directory.

Parameters:
localFile - the local file to upload
remoteFile - the remote destination file
off - the local file offset in bytes
Throws:
SftpException - if an I/O or SSH error occurs
See Also:
setLocalDir(File)

uploadDir

public void uploadDir(java.io.File localDirectory)
               throws SftpException
Uploads local directory and contents recursively to SSH server. Uploads are stored relative to current remote directory.

Parameters:
localDirectory - the directory to upload
Throws:
SftpException - if an I/O or SSH error occurs.

uploadDir

public void uploadDir(java.io.File localDirectory,
                      int retryLimit,
                      int threadCount)
               throws SftpException
Uploads local directory and contents recursively to SSH server. Uploads are stored relative to current remote directory.

Parameters:
localDirectory - the directory to upload
threadCount - the number of threads to spawn for upload
retryLimit - number of times to retry if upload fails
Throws:
SftpException - if an I/O or SSH error occurs.

uploadDir

public void uploadDir(java.io.File localDir,
                      int retryLimit)
               throws SftpException
Uploads local directory and contents. Uploads are stored relative to current remote directory.

Parameters:
localDir - the directory to upload
retryLimit - number of times to retry if upload fails
Throws:
SftpException - if an I/O or SSH error occurs.

uploadDir

public void uploadDir(java.io.File localDir,
                      int retryLimit,
                      java.lang.String extension)
               throws SftpException
Uploads local directory and contents. Uploads are stored relative to current remote directory.

Parameters:
localDir - the directory to upload
retryLimit - number of times to retry if upload fails
extension - the temporary extension which will be used to upload the files or null for default extension
Throws:
SftpException - if an I/O or SSH error occurs.

uploadDir

public void uploadDir(java.io.File localDir,
                      int retryLimit,
                      java.lang.String extension,
                      int threadCount)
               throws SftpException
Uploads local directory and contents. Uploads are stored relative to current remote directory.

Parameters:
localDir - the directory to upload
retryLimit - number of times to retry if upload fails
extension - the temporary extension which will be used to upload the files or null for default extension
threadCount - the number of new threads to spawn for upload.
Throws:
SftpException - if an I/O or SSH error occurs.

uploadDir

public void uploadDir(java.io.File localDir,
                      int retryLimit,
                      int retryInterval,
                      java.lang.String extension)
               throws SftpException
Uploads local directory and contents. Uploads are stored relative to current remote directory.

Parameters:
localDir - the directory to upload
retryLimit - number of times to retry if upload fails
retryInterval - retry interval in seconds
extension - the temporary extension which will be used to upload the files or null for default extension
Throws:
SftpException - if an I/O or SSH error occurs.

uploadDir

public void uploadDir(java.io.File localDir,
                      int retryLimit,
                      int retryInterval,
                      java.lang.String extension,
                      int threadCount)
               throws SftpException
Uploads local directory and contents. Uploads are stored relative to current remote directory.

Parameters:
localDir - the directory to upload
retryLimit - number of times to retry if upload fails
retryInterval - retry interval in seconds
extension - the temporary extension which will be used to upload the files or null for default extension
threadCount - the upload thread count.
Throws:
SftpException - if an I/O or SSH error occurs.

abortUploadThreads

public void abortUploadThreads()
Abort all upload threads.


uploadDir

public void uploadDir(java.io.File localDirectory,
                      java.lang.String remoteDirName,
                      java.lang.String extension)
               throws SftpException
Uploads local directory and contents recursively to SSH server.

Parameters:
localDirectory - the directory to upload
remoteDirName - the remote directory name
extension - The extension which will be used to upload the files
Throws:
SftpException - if an I/O or SSH error occurs.

mupload

public void mupload(java.util.Enumeration fileNames)
             throws SftpException
Uploads multiple files to SSH server. Uploads are performed relative to current local directory.

Parameters:
fileNames - filenames of files to upload
Throws:
SftpException - if an I/O or SSH error occurs.

mupload

public void mupload(java.lang.String regex)
             throws SftpException
Uploads multiple files matching filter to SSH server's current directory. Uploads are performed relative to current local directory. Regular expressions may be used to specify multiple files e.g. ".*\\.txt" may be used to upload all files with a .txt extension.

Parameters:
regex - the regular expression to use
Throws:
SftpException - if an I/O or SSH error occurs.

getHostKeys

public SshHostKeys getHostKeys()
Gets SSH host keys for further use.

Returns:
SSH host keys or null if keys are not available

getRecursiveDirectoryFileCount

public int getRecursiveDirectoryFileCount(java.lang.String directory)
Get size of all files rescursively in a given remote directory.

Parameters:
directory - - the remote directory name
Returns:
size of files.

getRecursiveDirectorySize

public long getRecursiveDirectorySize(java.lang.String directory)
Get size of all files rescursively in a given remote directory.

Parameters:
directory - - the remote directory name
Returns:
size of files.

getRemoteFileList

public java.util.Vector getRemoteFileList(java.lang.String remoteDir)
                                   throws SftpException
Gets a remote file list fron the current directory.

Parameters:
remoteDir - Remote directory name
Returns:
A collection of file paths.
Throws:
SftpException - If a IO error occurs

Secure FTP Factory

Copyright © JSCAPE LLC. 1999-2011. All Rights Reserved