public class ChannelSftp extends Channel
This class supports the client side of the sftp protocol,
version 3, and implements an interface similar to the
usual sftp
command line client.
The Sftp class in the examples directory implements a command line client based on this class.
This sftp client has the concept of a current local directory and a current remote directory. These are not inherent to the protocol, but are used implicitely for all path-based commands sent to the server (for the remote directory) or accessing the local file system (for the local directory).
They can be queried by lpwd()
and pwd()
, and
changed by cd(dir)
and lcd(dir)
.
Modifier and Type | Class and Description |
---|---|
class |
ChannelSftp.LsEntry
Represents a directory entry, representing a remote file or directory.
|
static interface |
ChannelSftp.LsEntrySelector
Objects implementing this interface can be passed as an argument for
ChannelSftp 's ls(java.lang.String) method. |
Modifier and Type | Field and Description |
---|---|
protected boolean |
agent_forwarding |
static int |
APPEND
file transfer mode: append to existing file, if any.
|
protected Hashtable |
env |
static int |
OVERWRITE
file transfer mode: overwrite the existing file, if any.
|
protected boolean |
pty |
static int |
RESUME
file transfer mode: resume an interrupted upload/download.
|
static int |
SSH_FX_BAD_MESSAGE |
static int |
SSH_FX_CONNECTION_LOST |
static int |
SSH_FX_EOF |
static int |
SSH_FX_FAILURE |
static int |
SSH_FX_NO_CONNECTION |
static int |
SSH_FX_NO_SUCH_FILE |
static int |
SSH_FX_OK |
static int |
SSH_FX_OP_UNSUPPORTED |
static int |
SSH_FX_PERMISSION_DENIED |
protected int |
tcol |
protected byte[] |
terminal_mode |
protected int |
thp |
protected int |
trow |
protected String |
ttype |
protected int |
twp |
protected boolean |
xforwading |
Constructor and Description |
---|
ChannelSftp() |
Modifier and Type | Method and Description |
---|---|
void |
_put(InputStream src,
String dst,
SftpProgressMonitor monitor,
int mode)
Not for external use.
|
void |
cd(String path)
Changes the current remote directory.
|
void |
chgrp(int gid,
String path)
Changes the owner group of one or several remote files.
|
void |
chmod(int permissions,
String path)
Changes the permissions of one or several remote files.
|
void |
chown(int uid,
String path)
Changes the owning user of one or several remote files.
|
void |
disconnect()
closes this channel.
|
void |
exit()
Exits the Channel.
|
InputStream |
get(String src)
Starts downloading a file as an InputStream.
|
InputStream |
get(String src,
int mode)
Deprecated.
This method will be deleted in the future.
A transfer mode is not meaningful here, as you can't really
overwrite or append an InputStream.
|
void |
get(String src,
OutputStream dst)
Downloads a file to an OutputStream.
|
void |
get(String src,
OutputStream dst,
SftpProgressMonitor monitor)
Downloads a file to an OutputStream.
|
void |
get(String src,
OutputStream dst,
SftpProgressMonitor monitor,
int mode,
long skip)
Downloads a file to an OutputStream.
|
InputStream |
get(String src,
SftpProgressMonitor monitor)
Starts downloading a file as an InputStream.
|
InputStream |
get(String src,
SftpProgressMonitor monitor,
int mode)
Deprecated.
This method will be deleted in the future.
A transfer mode is not meaningful here, as you can't really
overwrite or append an InputStream.
|
InputStream |
get(String src,
SftpProgressMonitor monitor,
long skip)
Starts downloading a file as an InputStream.
|
void |
get(String src,
String dst)
Downloads a file.
|
void |
get(String src,
String dst,
SftpProgressMonitor monitor)
Downloads a file.
|
void |
get(String src,
String dst,
SftpProgressMonitor monitor,
int mode)
Downloads a file.
|
int |
getBulkRequests()
This method will return the value how many requests may be
sent at any one time.
|
String |
getExtension(String key)
returns the extension data sent by the server
corresponding to some extension name.
|
String |
getHome()
returns the absolute path of the remote home directory.
|
int |
getServerVersion()
returns the server's protocol version number.
|
void |
hardlink(String oldpath,
String newpath) |
void |
lcd(String path)
Changes the current local directory.
|
String |
lpwd()
returns the current local directory
in absolute form.
|
Vector |
ls(String path)
lists the contents of a remote directory.
|
void |
ls(String path,
ChannelSftp.LsEntrySelector selector)
List files specified by the remote
path . |
SftpATTRS |
lstat(String path)
Retrieves the file attributes of a file or directory.
|
void |
mkdir(String path)
creates a new remote directory.
|
void |
put(InputStream src,
String dst)
Uploads a file from an input stream.
|
void |
put(InputStream src,
String dst,
int mode)
Uploads a file from an input stream.
|
void |
put(InputStream src,
String dst,
SftpProgressMonitor monitor)
Uploads a file from an input stream.
|
void |
put(InputStream src,
String dst,
SftpProgressMonitor monitor,
int mode)
Uploads a file from an input stream.
|
OutputStream |
put(String dst)
Starts an upload by OutputStream.
|
OutputStream |
put(String dst,
int mode)
Starts an upload by OutputStream.
|
OutputStream |
put(String dst,
SftpProgressMonitor monitor,
int mode)
Starts an upload by OutputStream.
|
OutputStream |
put(String dst,
SftpProgressMonitor monitor,
int mode,
long offset)
Starts an upload by OutputStream.
|
void |
put(String src,
String dst)
Uploads a file.
|
void |
put(String src,
String dst,
int mode)
Uploads a file.
|
void |
put(String src,
String dst,
SftpProgressMonitor monitor)
Uploads a file.
|
void |
put(String src,
String dst,
SftpProgressMonitor monitor,
int mode)
Uploads a file.
|
String |
pwd()
returns the current remote directory
in absolute form.
|
void |
quit()
Quits the Channel.
|
String |
readlink(String path)
reads a symbolic link.
|
String |
realpath(String path)
converts a remote path to its absolute (and to a
certain degree canonical) version.
|
void |
rename(String oldpath,
String newpath)
Renames a file or directory.
|
void |
rm(String path)
removes one or several files.
|
void |
rmdir(String path)
Removes one or several remote directories.
|
void |
run()
not to be called by the application.
|
protected void |
sendRequests()
creates and sends all requests indicated by the
properties
agentForwarding ,
xForwarding , pty
(and ptyType ) and
env . |
void |
setAgentForwarding(boolean enable)
Enable the agent forwarding.
|
void |
setBulkRequests(int bulk_requests)
Specify how many requests may be sent at any one time.
|
void |
setEnv(byte[] name,
byte[] value)
Set the environment variable.
|
void |
setEnv(Hashtable env)
Deprecated.
Use
setEnv(String, String) or setEnv(byte[], byte[]) instead. |
void |
setEnv(String name,
String value)
Set the environment variable.
|
void |
setFilenameEncoding(String encoding)
Sets the encoding used to convert file names from Strings to bytes.
|
void |
setMtime(String path,
int mtime)
sets the modification time of one or several remote files.
|
void |
setPty(boolean enable)
Allocate a Pseudo-Terminal.
|
void |
setPtySize(int col,
int row,
int wp,
int hp)
Change the window dimension interactively.
|
void |
setPtyType(String ttype)
Set the terminal type.
|
void |
setPtyType(String ttype,
int col,
int row,
int wp,
int hp)
Set the terminal type.
|
void |
setStat(String path,
SftpATTRS attr)
Changes attributes of a remote file or directory.
|
void |
setTerminalMode(byte[] terminal_mode)
Set the terminal mode.
|
void |
setXForwarding(boolean enable)
Enable the X11 forwarding.
|
void |
start()
Not to be called externally.
|
SftpATTRS |
stat(String path)
Retrieves the file attributes of a file or directory.
|
SftpStatVFS |
statVFS(String path) |
void |
symlink(String oldpath,
String newpath)
Creates a new symbolic link.
|
String |
version()
returns the protocol version number supported by this client.
|
connect, connect, genChannelOpenPacket, getExitStatus, getExtInputStream, getId, getInputStream, getOutputStream, getSession, isClosed, isConnected, isEOF, sendChannelOpen, sendOpenConfirmation, sendOpenFailure, sendSignal, setExtOutputStream, setExtOutputStream, setInputStream, setInputStream, setOutputStream, setOutputStream
public static final int SSH_FX_OK
public static final int SSH_FX_EOF
public static final int SSH_FX_NO_SUCH_FILE
public static final int SSH_FX_PERMISSION_DENIED
public static final int SSH_FX_FAILURE
public static final int SSH_FX_BAD_MESSAGE
public static final int SSH_FX_NO_CONNECTION
public static final int SSH_FX_CONNECTION_LOST
public static final int SSH_FX_OP_UNSUPPORTED
public static final int OVERWRITE
public static final int RESUME
public static final int APPEND
protected boolean agent_forwarding
protected boolean xforwading
protected Hashtable env
protected boolean pty
protected String ttype
protected int tcol
protected int trow
protected int twp
protected int thp
protected byte[] terminal_mode
public void setBulkRequests(int bulk_requests) throws JSchException
bulk_requests
- how many requests may be outstanding at any one time.JSchException
public int getBulkRequests()
public void start() throws JSchException
Channel
Channel.connect(int)
when the connection
is established.
This implementation here does nothing, but it is overridden in some
subclasses.start
in class Channel
JSchException
public void quit()
disconnect()
.public void exit()
disconnect()
.public void lcd(String path) throws SftpException
path
- a directory path, absolute or relative to the
current local path.SftpException
- if the mentioned path is not a directory.public void cd(String path) throws SftpException
path
- a directory path, absolute or relative to the
current remote path.SftpException
- if the named path does not indicate a directory,
if it is not accessible by the user, or some other problem occurs.public void put(String src, String dst) throws SftpException
OVERWRITE
mode, and no
progress monitor.src
- the local source file name, absolute or relative to the
current local directory.dst
- the remote destination file name, absolute or relative to the
current remote directory.SftpException
put(String,String,SftpProgressMonitor,int)
public void put(String src, String dst, int mode) throws SftpException
src
- the local source file name, absolute or relative to the
current local directory.dst
- the remote destination file name, absolute or relative to the
current remote directory.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
.SftpException
- if some problem occurred.put(String,String,SftpProgressMonitor,int)
public void put(String src, String dst, SftpProgressMonitor monitor) throws SftpException
OVERWRITE
mode.src
- the local source file name, absolute or relative to the
current local directory.dst
- the remote destination file name, absolute or relative to the
current remote directory.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.SftpException
- if some problem occurred.put(String,String,SftpProgressMonitor,int)
public void put(String src, String dst, SftpProgressMonitor monitor, int mode) throws SftpException
src
- the local source file name, absolute or relative to the
current local directory.dst
- the remote destination file name, absolute or relative to the
current remote directory.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
.SftpException
- if some problem occurred.public void put(InputStream src, String dst) throws SftpException
OVERWRITE
mode and no progress monitor.src
- the source file, in form of an input stream.dst
- the remote destination file name, relative to the
current remote directory.SftpException
- if some problem occurred.put(InputStream, String, SftpProgressMonitor, int)
public void put(InputStream src, String dst, int mode) throws SftpException
src
- the source file, in form of an input stream.dst
- the remote destination file name, relative to the
current remote directory.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
.SftpException
- if some problem occurred.put(InputStream, String, SftpProgressMonitor, int)
public void put(InputStream src, String dst, SftpProgressMonitor monitor) throws SftpException
OVERWRITE
mode.src
- the source file, in form of an input stream.dst
- the remote destination file name, relative to the
current remote directory.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.SftpException
- if some problem occurred.put(InputStream, String, SftpProgressMonitor, int)
public void put(InputStream src, String dst, SftpProgressMonitor monitor, int mode) throws SftpException
src
- the source file, in form of an input stream.dst
- the remote destination file name, relative to the
current remote directory.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
.SftpException
- if some problem occurred.public void _put(InputStream src, String dst, SftpProgressMonitor monitor, int mode) throws SftpException
put
methods, without some of
the checks.
This method should not be public.SftpException
public OutputStream put(String dst) throws SftpException
OVERWRITE
mode, no progress monitor
and an offset of 0.dst
- the remote destination file name, relative to the
current remote directory.SftpException
- if some problem occurred.put(String, SftpProgressMonitor, int, long)
public OutputStream put(String dst, int mode) throws SftpException
dst
- the remote destination file name, relative to the
current remote directory.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
.SftpException
- if some problem occurred.put(String, SftpProgressMonitor, int, long)
public OutputStream put(String dst, SftpProgressMonitor monitor, int mode) throws SftpException
dst
- the remote destination file name, relative to the
current remote directory.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
.SftpException
- if some problem occurred.put(String, SftpProgressMonitor, int, long)
public OutputStream put(String dst, SftpProgressMonitor monitor, int mode, long offset) throws SftpException
The returned output stream should be used by the application to write data, which will then be uploaded to the remote file. Closing the stream will finish the upload.
dst
- the remote destination file name, relative to the
current remote directory.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
.offset
- the position in the remote file where we want
to start writing. In the RESUME
and APPEND
modes,
this is added to the current size of the file (i.e. then a value > 0
creates a sparse section in the file).
(I hope I did understand this right - there is no example using this
method with offset.)SftpException
- if some problem occurred.public void get(String src, String dst) throws SftpException
OVERWRITE
mode and no progress monitor.SftpException
get(String, String, SftpProgressMonitor, int)
public void get(String src, String dst, SftpProgressMonitor monitor) throws SftpException
OVERWRITE
mode.SftpException
get(String, String, SftpProgressMonitor, int)
public void get(String src, String dst, SftpProgressMonitor monitor, int mode) throws SftpException
src
- the source file name, relative to the
current remote directory.dst
- the destination file name, relative to the
current local directory.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
.SftpException
- if some problem occurred.public void get(String src, OutputStream dst) throws SftpException
OVERWRITE
mode and no progress monitor.SftpException
get(String, OutputStream, SftpProgressMonitor, int, long)
public void get(String src, OutputStream dst, SftpProgressMonitor monitor) throws SftpException
OVERWRITE
mode.SftpException
get(String, OutputStream, SftpProgressMonitor, int, long)
public void get(String src, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) throws SftpException
src
- the source file name, relative to the
current remote directory.dst
- the destination output stream.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.mode
- the transfer mode, one of RESUME
, APPEND
,
OVERWRITE
. The difference is that RESUME
mode uses
the skip
argument, while OVERWRITE
and APPEND
ignore it.skip
- the position in the remote file where we should
start the download, if we are in RESUME
mode.SftpException
- if some problem occurred.public InputStream get(String src) throws SftpException
SftpException
get(String, SftpProgressMonitor, long)
public InputStream get(String src, SftpProgressMonitor monitor) throws SftpException
SftpException
get(String, SftpProgressMonitor, long)
public InputStream get(String src, int mode) throws SftpException
SftpException
get(String)
,
get(String, SftpProgressMonitor, long)
public InputStream get(String src, SftpProgressMonitor monitor, int mode) throws SftpException
SftpException
get(String, SftpProgressMonitor, long)
,
get(String, SftpProgressMonitor)
public InputStream get(String src, SftpProgressMonitor monitor, long skip) throws SftpException
src
- the source file name, relative to the
current remote directory.monitor
- an object receiving notifications about the progress
of the operation. Can be null
, then there will be no progress
notification.skip
- the position in the remote file where we should
start the download.SftpException
- if some problem occurred.public Vector ls(String path) throws SftpException
path
- a pattern relative to the
current remote directory.
The pattern can contain glob pattern wildcards (*
or ?
)
in the last component (i.e. after the last /
).ChannelSftp.LsEntry
objects.SftpException
public void ls(String path, ChannelSftp.LsEntrySelector selector) throws SftpException
path
.
Each files and directories will be passed to
LsEntrySelector#select(LsEntry)
method, and if that method
returns LsEntrySelector#BREAK
, the operation will be
canceled immediately.SftpException
ChannelSftp.LsEntrySelector
public String readlink(String path) throws SftpException
path
- a path relative to the
current remote directory,
which should correspond to a symbolic link.SftpException
public void symlink(String oldpath, String newpath) throws SftpException
Note: The protocol draft declares the two parameters in the reverse order (i.e. first linkpath, then targetpath), but because of an erronous implementation in (both sides of) OpenSSH, the de facto protocol is now what is implemented here (first targetpath, then linkpath). If you are speaking to a SFTP server which implements the protocol as specified, you might have to swap the arguments.
oldpath
- the path of the link target, relative to the
current remote directorynewpath
- the path of the link to be created, relative to the
current remote directorySftpException
public void hardlink(String oldpath, String newpath) throws SftpException
SftpException
public void rename(String oldpath, String newpath) throws SftpException
oldpath
- the old name of the file, relative to the
current remote directory.newpath
- the new name of the file, relative to the
current remote directory.SftpException
public void rm(String path) throws SftpException
path
- a glob pattern of the files to be removed, relative to the
current remote directory.SftpException
public void chgrp(int gid, String path) throws SftpException
gid
- the identifier of the new group.path
- a glob pattern of the files to be reowned, relative to the
current remote directory.SftpException
public void chown(int uid, String path) throws SftpException
uid
- the identifier of the new owner.path
- a glob pattern of the files to be reowned, relative to the
current remote directory.SftpException
public void chmod(int permissions, String path) throws SftpException
permissions
- the new permission pattern.
This may be modified by a current mask before being applied.path
- a glob pattern of the files to be reowned, relative to the
current remote directory.SftpException
public void setMtime(String path, int mtime) throws SftpException
path
- a glob pattern of the files to be reowned, relative to the
current remote directory.mtime
- the new modification time, in seconds from
the unix epoch.SftpException
public void rmdir(String path) throws SftpException
path
- a glob pattern of the directories to be removed, relative
to the current remote directory.SftpException
public void mkdir(String path) throws SftpException
path
- the path of the new directory, relative
to the current remote directory.SftpException
public SftpATTRS stat(String path) throws SftpException
path
- the path of the file or directory, relative
to the current remote directory.SftpException
lstat(String)
public SftpStatVFS statVFS(String path) throws SftpException
SftpException
public SftpATTRS lstat(String path) throws SftpException
path
- the path of the file or directory, relative
to the current remote directory.SftpException
stat(String)
public void setStat(String path, SftpATTRS attr) throws SftpException
path
- the path of the file or directory, relative
to the current remote directory.attr
- the attribute set containing the attributes to be changed.SftpException
public String pwd() throws SftpException
SftpException
cd(java.lang.String)
public String lpwd()
lcd(java.lang.String)
public String version()
public String getHome() throws SftpException
SftpException
public void disconnect()
disconnect
in class Channel
public int getServerVersion() throws SftpException
SftpException
public void setFilenameEncoding(String encoding) throws SftpException
"UTF-8"
.
I have no idea what is the idea behind this.
This method is now used nowhere.SftpException
public String getExtension(String key)
public String realpath(String path) throws SftpException
path
- a path name, relative to the
current local directory.SftpException
public void setAgentForwarding(boolean enable)
enable
- public void setXForwarding(boolean enable)
setXForwarding
in class Channel
enable
- public void setEnv(Hashtable env)
setEnv(String, String)
or setEnv(byte[], byte[])
instead.setEnv(String, String)
,
setEnv(byte[], byte[])
public void setEnv(String name, String value)
name
and value
are needed to be passed
to the remote in your favorite encoding,
use setEnv(byte[], byte[])
.
Refer to RFC4254 6.4 Environment Variable Passing.name
- A name for environment variable.value
- A value for environment variable.public void setEnv(byte[] name, byte[] value)
name
- A name of environment variable.value
- A value of environment variable.setEnv(String, String)
public void setPty(boolean enable)
enable
- public void setTerminalMode(byte[] terminal_mode)
terminal_mode
- public void setPtySize(int col, int row, int wp, int hp)
col
- terminal width, columnsrow
- terminal height, rowswp
- terminal width, pixelshp
- terminal height, pixelspublic void setPtyType(String ttype)
ttype
- terminal type(for example, "vt100")setPtyType(String, int, int, int, int)
public void setPtyType(String ttype, int col, int row, int wp, int hp)
ttype
- terminal type(for example, "vt100")col
- terminal width, columnsrow
- terminal height, rowswp
- terminal width, pixelshp
- terminal height, pixelsprotected void sendRequests() throws Exception
agentForwarding
,
xForwarding
, pty
(and ptyType
) and
env
.Exception
This is an inofficial Javadoc created by PaĆlo Ebermann. Have a look at the official homepage.