Class SftpClient
Implements a Secure File Transfer (SFTP) client.
- Since:
- 0.2.0
- Version:
- $Revision: 1.44 $
- Author:
- Lee David Painter
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addEventListener
(ChannelEventListener eventListener) void
Changes the working directory on the remote server.void
Sets the group ID for the file or directory.void
Changes the access permissions or modes of the specified file or directory.void
Sets the user ID to owner for the file or directory.copyLocalDirectory
(String localdir, String remotedir, boolean recurse, boolean sync, boolean commit, FileTransferProgress progress) copyRemoteDirectory
(String remotedir, String localdir, boolean recurse, boolean sync, boolean commit, FileTransferProgress progress) get
(String path, FileTransferProgress progress) Download the remote file to the local computer.get
(String remote, OutputStream local) get
(String remote, OutputStream local, FileTransferProgress progress) Download the remote file writing it to the specifiedOutputStream
.get
(String remote, String local, FileTransferProgress progress) Download the remote file to the local computer.getAbsolutePath
(String path) boolean
isClosed()
Returns the state of the SFTP client.void
Changes the local working directory.lpwd()
Returns the absolute path to the local working directory.ls()
List the contents of the current remote working directory.List the contents remote directory.void
Creates a new directory on the remote server.void
Create a directory or set of directories.void
put
(InputStream in, String remote) void
put
(InputStream in, String remote, FileTransferProgress progress) Upload a file to the remote computer reading from the specifiedInputStream
.void
void
put
(String local, FileTransferProgress progress) Upload a file to the remote computer.void
void
put
(String local, String remote, FileTransferProgress progress) Upload a file to the remote computer.pwd()
Returns the absolute path name of the current remote working directory.void
quit()
Close the SFTP client.void
Rename a file on the remote computer.void
Remove a file or directory from the remote computer.void
Returns the attributes of the file from the remote computer.void
Create a symbolic link on the remote computer.int
umask
(int umask) Sets the umask used by this client.void
-
Method Details
-
umask
public int umask(int umask) Sets the umask used by this client.- Parameters:
umask
-- Returns:
- the previous umask value
-
cd
Changes the working directory on the remote server.
- Parameters:
dir
- the new working directory- Throws:
IOException
- if an IO error occurs or the file does not existFileNotFoundException
- Since:
- 0.2.0
-
mkdir
Creates a new directory on the remote server. This method will throw an exception if the directory already exists. To create directories and disregard any errors use the
mkdirs
method.- Parameters:
dir
- the name of the new directory- Throws:
IOException
- if an IO error occurs or if the directory already exists- Since:
- 0.2.0
-
mkdirs
Create a directory or set of directories. This method will not fail even if the directories exist. It is advisable to test whether the directory exists before attempting an operation by using the
stat
method to return the directories attributes.- Parameters:
dir
- the path of directories to create.
-
pwd
Returns the absolute path name of the current remote working directory.
- Returns:
- the absolute path of the remote working directory.
- Since:
- 0.2.0
-
ls
List the contents of the current remote working directory.
Returns a list of
SftpFile
instances for the current working directory.- Returns:
- a list of SftpFile for the current working directory
- Throws:
IOException
- if an IO error occurs- Since:
- 0.2.0
- See Also:
-
ls
List the contents remote directory.
Returns a list of
SftpFile
instances for the remote directory.- Parameters:
path
- the path on the remote server to list- Returns:
- a list of SftpFile for the remote directory
- Throws:
IOException
- if an IO error occurs- Since:
- 0.2.0
- See Also:
-
lcd
Changes the local working directory.
- Parameters:
path
- the path to the new working directory- Throws:
IOException
- if an IO error occurs- Since:
- 0.2.0
-
lpwd
Returns the absolute path to the local working directory.
- Returns:
- the absolute path of the local working directory.
- Since:
- 0.2.0
-
get
public FileAttributes get(String path, FileTransferProgress progress) throws IOException, TransferCancelledException Download the remote file to the local computer.
- Parameters:
path
- the path to the remote fileprogress
-- Returns:
- Throws:
IOException
- if an IO error occurs of the file does not existTransferCancelledException
- Since:
- 0.2.0
-
get
- Parameters:
path
-- Returns:
- Throws:
IOException
-
get
public FileAttributes get(String remote, String local, FileTransferProgress progress) throws IOException, TransferCancelledException Download the remote file to the local computer. If the paths provided are not absolute the current working directory is used.
- Parameters:
remote
- the path/name of the remote filelocal
- the path/name to place the file on the local computerprogress
-- Returns:
- Throws:
IOException
- if an IO error occurs or the file does not existTransferCancelledException
- Since:
- 0.2.0
-
get
- Parameters:
remote
-local
-- Returns:
- Throws:
IOException
-
get
public FileAttributes get(String remote, OutputStream local, FileTransferProgress progress) throws IOException, TransferCancelledException Download the remote file writing it to the specified
OutputStream
. The OutputStream is closed by this mehtod even if the operation fails.- Parameters:
remote
- the path/name of the remote filelocal
- the OutputStream to writeprogress
-- Returns:
- Throws:
IOException
- if an IO error occurs or the file does not existTransferCancelledException
- Since:
- 0.2.0
-
get
- Parameters:
remote
-local
-- Returns:
- Throws:
IOException
-
isClosed
public boolean isClosed()Returns the state of the SFTP client. The client is closed if the underlying session channel is closed. Invoking the
quit
method of this object will close the underlying session channel.- Returns:
- true if the client is still connected, otherwise false
- Since:
- 0.2.0
-
put
public void put(String local, FileTransferProgress progress) throws IOException, TransferCancelledException Upload a file to the remote computer.
- Parameters:
local
- the path/name of the local fileprogress
-- Throws:
IOException
- if an IO error occurs or the file does not existTransferCancelledException
- Since:
- 0.2.0
-
put
- Parameters:
local
-- Throws:
IOException
-
put
public void put(String local, String remote, FileTransferProgress progress) throws IOException, TransferCancelledException Upload a file to the remote computer. If the paths provided are not absolute the current working directory is used.
- Parameters:
local
- the path/name of the local fileremote
- the path/name of the destination fileprogress
-- Throws:
IOException
- if an IO error occurs or the file does not existTransferCancelledException
- Since:
- 0.2.0
-
put
- Parameters:
local
-remote
-- Throws:
IOException
-
put
public void put(InputStream in, String remote, FileTransferProgress progress) throws IOException, TransferCancelledException Upload a file to the remote computer reading from the specified
InputStream
. The InputStream is closed, even if the operation fails.- Parameters:
in
- the InputStream being readremote
- the path/name of the destination fileprogress
-- Throws:
IOException
- if an IO error occursTransferCancelledException
- Since:
- 0.2.0
-
put
- Parameters:
in
-remote
-- Throws:
IOException
-
chown
Sets the user ID to owner for the file or directory.
- Parameters:
uid
- numeric user id of the new ownerpath
- the path to the remote file/directory- Throws:
IOException
- if an IO error occurs or the file does not exist- Since:
- 0.2.0
-
chgrp
Sets the group ID for the file or directory.
- Parameters:
gid
- the numeric group id for the new grouppath
- the path to the remote file/directory- Throws:
IOException
- if an IO error occurs or the file does not exist- Since:
- 0.2.0
-
chmod
Changes the access permissions or modes of the specified file or directory.
Modes determine who can read, change or execute a file.
Absolute modes are octal numbers specifying the complete list of attributes for the files; you specify attributes by OR'ing together these bits. 0400 Individual read 0200 Individual write 0100 Individual execute (or list directory) 0040 Group read 0020 Group write 0010 Group execute 0004 Other read 0002 Other write 0001 Other execute
- Parameters:
permissions
- the absolute mode of the file/directorypath
- the path to the file/directory on the remote server- Throws:
IOException
- if an IO error occurs or the file if not found- Since:
- 0.2.0
-
umask
- Throws:
IOException
-
rename
Rename a file on the remote computer.
- Parameters:
oldpath
- the old pathnewpath
- the new path- Throws:
IOException
- if an IO error occurs- Since:
- 0.2.0
-
rm
Remove a file or directory from the remote computer.
- Parameters:
path
- the path of the remote file/directory- Throws:
IOException
- if an IO error occurs- Since:
- 0.2.0
-
rm
- Parameters:
path
-force
-recurse
-- Throws:
IOException
-
symlink
Create a symbolic link on the remote computer.
- Parameters:
path
- the path to the existing filelink
- the new link- Throws:
IOException
- if an IO error occurs or the operation is not supported on the remote platform- Since:
- 0.2.0
-
stat
Returns the attributes of the file from the remote computer.
- Parameters:
path
- the path of the file on the remote computer- Returns:
- the attributes
- Throws:
IOException
- if an IO error occurs or the file does not exist- Since:
- 0.2.0
- See Also:
-
getAbsolutePath
- Parameters:
path
-- Returns:
- Throws:
IOException
-
quit
Close the SFTP client.
- Throws:
IOException
- Since:
- 0.2.0
-
copyLocalDirectory
public DirectoryOperation copyLocalDirectory(String localdir, String remotedir, boolean recurse, boolean sync, boolean commit, FileTransferProgress progress) throws IOException - Parameters:
localdir
-remotedir
-recurse
-sync
-commit
-progress
-- Returns:
- Throws:
IOException
-
addEventListener
- Parameters:
eventListener
-
-
copyRemoteDirectory
public DirectoryOperation copyRemoteDirectory(String remotedir, String localdir, boolean recurse, boolean sync, boolean commit, FileTransferProgress progress) throws IOException - Parameters:
remotedir
-localdir
-recurse
-sync
-commit
-progress
-- Returns:
- Throws:
IOException
-