Secure FTP Factory

com.jscape.inet.scp
Class Scp

java.lang.Object
  extended by com.jscape.inet.scp.Scp

public final class Scp
extends java.lang.Object

Implements the basic functionality of an SCP (Secure Copy) client over SSH2 protocol.

Example Usage:

 // create new SshParamters instance
 SshParameters params = new SshParameters(hostname,username,password);
 

// create new Scp instance Scp scp = new Scp(params);

// register event listener scp.addListener(this);

// establish connection scp.connect();

// upload file to remote directory scp.upload(new File("c:/tmp/file.txt"),"/home/jsmith/");

// disconnect scp.disconnect();


Constructor Summary
Scp(SshParameters parameters)
          Creates a new client instance.
Scp(SshParameters parameters, SshConfiguration configuration)
          Creates a new client instance.
 
Method Summary
 void addListener(ScpEventListener listener)
          Adds an event listener.
 void connect()
          Connects to the server.
 void disconnect()
          Disconnects from the server.
 void download(java.io.OutputStream out, java.lang.String remoteDir, java.lang.String remoteFile)
          Downloads file from SSH server and writes to specified output stream.
 java.io.File download(java.lang.String remoteDir, java.lang.String remoteFile)
          Downloads the file from SSH server.
 void downloadDir(java.lang.String remoteDir, java.lang.String name)
          Downloads remote directory and contents recursively from SSH server.
 SshConfiguration getConfiguration()
          Returns current SSH configuraton.
 boolean getDebug()
          Returns current debug status.
 java.io.PrintStream getDebugStream()
          Gets PrintStream used in reporting debugging statements.
 int getDownloadBlockSize()
          Returns download block size in bytes.
 java.lang.String getHost()
          Returns server host name/IP.
 SshHostKeys getHostKeys()
          Returns SSH host keys for further use.
 java.io.File getLocalDir()
          Returns current local directory.
 SshParameters getParameters()
          Returns current SSH parameters.
 int getPort()
          Returns server port.
 boolean getPreserveTime()
          Checks if preserve time flag is set.
 int getUploadBlockSize()
          Returns upload block size in bytes.
 void interrupt()
          Interrupts current transfer process.
 boolean isConnected()
          Checks if client is connected.
 void mdownload(java.lang.String remoteDir, java.util.Enumeration fileNames)
          Downloads specified files in current working remote directory from SSH server to local directory.
 void mdownload(java.lang.String remoteDir, java.util.Iterator fileNames)
          Downloads specified files in current working remote directory from SSH server to local directory.
 void mdownload(java.lang.String remoteDir, java.lang.String filter)
          Downloads multiple files matching filter from SSH server's directory.
 void mdownload(java.lang.String remoteDir, java.lang.String[] fileNames)
          Downloads specified files in current working remote directory from SSH server to local directory.
 void mupload(java.lang.String remoteDir, java.util.Enumeration fileNames)
          Uploads multiple files to SSH server.
 void mupload(java.lang.String remoteDir, java.util.Iterator fileNames)
          Uploads multiple files to SSH server.
 void mupload(java.lang.String remoteDir, java.lang.String filter)
          Uploads multiple files matching regular expression to SSH server's current directory.
 void mupload(java.lang.String remoteDir, java.lang.String[] fileNames)
          Uploads multiple files to SSH server.
 void removeListener(ScpEventListener listener)
          Removes listener.
 void setDebug(boolean value)
          Sets debug status.
 void setDebugStream(java.io.PrintStream value)
          Sets PrintStream used in reporting debugging statements.
 void setDownloadBlockSize(int value)
          Sets download block size.
 void setLocalDir(java.io.File value)
          Sets current local directory.
 void setPreserveTime(boolean value)
          Sets preserver time flag.
 void setReceiveBufferSize(int receiveBufferSize)
          Sets the receive buffer size option of the scp connection.
 void setSendBufferSize(int sendBufferSize)
          Sets the send buffer size option of the scp connection.
 void setUploadBlockSize(int value)
          Sets upload block size.
 void upload(byte[] data, java.lang.String remoteDir, java.lang.String remoteFile)
          Uploads file to SSH server where contents of file is not on disk but in memory.
 void upload(java.io.File file, java.lang.String remoteDir)
          Uploads file to SSH server.
 void upload(java.io.File file, java.lang.String remoteDir, java.lang.String remoteFile)
          Uploads file to SSH server.
 void upload(java.io.InputStream in, long fileLength, java.lang.String remoteDir, java.lang.String remoteFile)
          Upload file to SSH server.
 void upload(java.io.InputStream in, java.lang.String remoteDir, java.lang.String remoteFile)
          Uploads file to SSH server.
 void upload(java.lang.String remoteDir, java.lang.String filename)
          Uploads file to SSH server.
 void upload(java.lang.String localFile, java.lang.String remoteDir, java.lang.String remoteFile)
          Uploads file to SSH server.
 void uploadDir(java.io.File dir, java.lang.String remoteDir)
          Uploads the specified directory.
 void uploadDir(java.io.File dir, java.lang.String remoteDir, java.lang.String remoteName)
          Uploads the specified directory.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Scp

public Scp(SshParameters parameters,
           SshConfiguration configuration)
Creates a new client instance.

Parameters:
parameters - ssh connection parameters
configuration - ssh configuration parameters

Scp

public Scp(SshParameters parameters)
Creates a new client instance.

Parameters:
parameters - ssh connection parameters
Method Detail

getParameters

public SshParameters getParameters()
Returns current SSH parameters.

Returns:
current SSH parameters

getConfiguration

public SshConfiguration getConfiguration()
Returns current SSH configuraton.

Returns:
current SSH configuraton

getHost

public java.lang.String getHost()
Returns server host name/IP.

Returns:
server host name/IP

getPort

public int getPort()
Returns server port.

Returns:
server port

getLocalDir

public java.io.File getLocalDir()
Returns current local directory.

Returns:
current local directory

setLocalDir

public void setLocalDir(java.io.File value)
Sets current local directory.

Parameters:
value - local directory

getUploadBlockSize

public int getUploadBlockSize()
Returns upload block size in bytes.

Returns:
upload block size in bytes

setUploadBlockSize

public void setUploadBlockSize(int value)
Sets upload block size.

Parameters:
value - upload block size in bytes

getDownloadBlockSize

public int getDownloadBlockSize()
Returns download block size in bytes.

Returns:
download block size in bytes

setDownloadBlockSize

public void setDownloadBlockSize(int value)
Sets download block size.

Parameters:
value - download block size in bytes

getPreserveTime

public boolean getPreserveTime()
Checks if preserve time flag is set.

Returns:
preserve flag value

setPreserveTime

public void setPreserveTime(boolean value)
Sets preserver time flag.

Parameters:
value - preserve time

setSendBufferSize

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

Parameters:
sendBufferSize - Send buffer size.

setReceiveBufferSize

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

Parameters:
receiveBufferSize - Receive buffer size.

getDebug

public boolean getDebug()
Returns current debug status.

Returns:
true if debug is on; false otherwise

setDebug

public void setDebug(boolean value)
Sets debug status.

Parameters:
value - if true debug output will bw turned on

setDebugStream

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

Parameters:
value - 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

addListener

public void addListener(ScpEventListener listener)
Adds an event listener.

Parameters:
listener - event listener

removeListener

public void removeListener(ScpEventListener listener)
Removes listener.

Parameters:
listener - target listener

getHostKeys

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

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

connect

public void connect()
             throws ScpException
Connects to the server.

Throws:
ScpException - if an I/O error occurs

disconnect

public void disconnect()
Disconnects from the server.


isConnected

public boolean isConnected()
Checks if client is connected.

Returns:
true if client is connected; false otherwise

interrupt

public void interrupt()
Interrupts current transfer process.


download

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

Parameters:
out - the OutputStream to writing file to
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
remoteFile - the remote file to download
Throws:
java.io.IOException - if an I/O, SSH or Scp error occurs.

download

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

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
remoteFile - the remote file to download
Returns:
the file downloaded
Throws:
java.io.IOException - if an I/O or SSH error occurs.

mdownload

public void mdownload(java.lang.String remoteDir,
                      java.util.Enumeration fileNames)
               throws java.io.IOException
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:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
fileNames - filenames of files to download
Throws:
java.io.IOException - if an I/O or SSH error occurs.
See Also:
downloadDir(String, String)

mdownload

public void mdownload(java.lang.String remoteDir,
                      java.util.Iterator fileNames)
               throws java.io.IOException
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:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
fileNames - filenames of files to download
Throws:
java.io.IOException - if an I/O or SSH error occurs.
See Also:
downloadDir(String, String)

mdownload

public void mdownload(java.lang.String remoteDir,
                      java.lang.String[] fileNames)
               throws java.io.IOException
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:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
fileNames - filenames of files to download
Throws:
java.io.IOException - if an I/O or SSH error occurs.
See Also:
downloadDir(String, String)

mdownload

public void mdownload(java.lang.String remoteDir,
                      java.lang.String filter)
               throws java.io.IOException
Downloads multiple files matching filter from SSH server's 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*".

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
filter - the filter to use
Throws:
java.io.IOException - if an I/O or SSH error occurs.
See Also:
downloadDir(java.lang.String, java.lang.String)

downloadDir

public void downloadDir(java.lang.String remoteDir,
                        java.lang.String name)
                 throws java.io.IOException
Downloads remote directory and contents recursively from SSH server. Downloads are stored relative to current local directory.

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
name - the directory to download
Throws:
java.io.IOException - if an I/O or SSH error occurs.

upload

public void upload(byte[] data,
                   java.lang.String remoteDir,
                   java.lang.String remoteFile)
            throws java.io.IOException
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
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
remoteFile - the filename to store file as on SSH server
Throws:
java.io.IOException - if an I/O or SSH error occurs.

upload

public void upload(java.io.InputStream in,
                   java.lang.String remoteDir,
                   java.lang.String remoteFile)
            throws java.io.IOException
Uploads file to SSH server. This operation uses InputStream.available() for computing data amount to send. For data amount more than 2 Gb use Scp.upload(InputStream in, long fileLength, String remoteDir, String remoteFile) method.

Parameters:
in - the input stream to read from
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
remoteFile - the remote file name relative to current remote directory
Throws:
java.io.IOException - if an I/O or SSH error occurs

upload

public void upload(java.io.InputStream in,
                   long fileLength,
                   java.lang.String remoteDir,
                   java.lang.String remoteFile)
            throws java.io.IOException
Upload file to SSH server.

Parameters:
in - the input stream to read from
fileLength - total amount of data to send
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
remoteFile - the remote file name relative to current remote directory
Throws:
java.io.IOException - if an error occurs

upload

public void upload(java.io.File file,
                   java.lang.String remoteDir)
            throws java.io.IOException
Uploads file to SSH server.

Parameters:
file - file to upload
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
Throws:
java.io.IOException - if an I/O or SSH error occurs

upload

public void upload(java.io.File file,
                   java.lang.String remoteDir,
                   java.lang.String remoteFile)
            throws java.io.IOException
Uploads file to SSH server.

Parameters:
file - file to upload
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
remoteFile - the remote file name relative to current remote directory
Throws:
java.io.IOException - if an I/O or SSH error occurs

upload

public void upload(java.lang.String remoteDir,
                   java.lang.String filename)
            throws java.io.IOException
Uploads file to SSH server.

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
filename - file name to upload
Throws:
java.io.IOException - if an I/O or SSH error occurs

upload

public void upload(java.lang.String localFile,
                   java.lang.String remoteDir,
                   java.lang.String remoteFile)
            throws java.io.IOException
Uploads file to SSH server.

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
localFile - local file name relative to local directory
remoteFile - the remote file name relative to current remote directory
Throws:
java.io.IOException - if an I/O or SSH error occurs
See Also:
setLocalDir(java.io.File)

mupload

public void mupload(java.lang.String remoteDir,
                    java.util.Enumeration fileNames)
             throws java.io.IOException
Uploads multiple files to SSH server. Uploads are performed relative to current local directory.

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
fileNames - filenames of files to upload
Throws:
java.io.IOException - if an I/O or SSH error occurs.

mupload

public void mupload(java.lang.String remoteDir,
                    java.util.Iterator fileNames)
             throws java.io.IOException
Uploads multiple files to SSH server. Uploads are performed relative to current local directory.

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
fileNames - filenames of files to upload
Throws:
java.io.IOException - if an I/O or SSH error occurs.

mupload

public void mupload(java.lang.String remoteDir,
                    java.lang.String[] fileNames)
             throws java.io.IOException
Uploads multiple files to SSH server. Uploads are performed relative to current local directory.

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
fileNames - filenames of files to upload
Throws:
java.io.IOException - if an I/O or SSH error occurs.

mupload

public void mupload(java.lang.String remoteDir,
                    java.lang.String filter)
             throws java.io.IOException
Uploads multiple files matching regular expression to SSH server's current directory. Uploads are performed relative to current local directory. For example, to upload local files with .txt extension use the regular expression ".*\.txt".

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
filter - the filter to use
Throws:
java.io.IOException - if an I/O or SSH error occurs.

uploadDir

public void uploadDir(java.io.File dir,
                      java.lang.String remoteDir,
                      java.lang.String remoteName)
               throws java.io.IOException
Uploads the specified directory.

Parameters:
dir - directory to upload
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
remoteName - the remote directory name relative to current remote directory
Throws:
java.io.IOException - if an I/O or SSH error occurs

uploadDir

public void uploadDir(java.io.File dir,
                      java.lang.String remoteDir)
               throws java.io.IOException
Uploads the specified directory.

Parameters:
remoteDir - remote directory path with ending path separator (i.e "/user/home/") or empty line for current directory
dir - directory to upload
Throws:
java.io.IOException - if an I/O or SSH error occurs

Secure FTP Factory

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