public class SFTPv3Client extends Object
SFTPv3Client represents a SFTP (protocol version 3)
client connection tunnelled over a SSH-2 connection. This is a very simple
(synchronous) implementation.
Basically, most methods in this class map directly to one of
the packet types described in draft-ietf-secsh-filexfer-02.txt.
Note: this is experimental code.
Error handling: the methods of this class throw IOExceptions. However, unless
there is catastrophic failure, exceptions of the type SFTPv3Client will
be thrown (a subclass of IOException). Therefore, you can implement more verbose
behavior by checking if a thrown exception if of this type. If yes, then you
can cast the exception and access detailed information about the failure.
Notes about file names, directory names and paths, copy-pasted
from the specs:
setCharset(String).| Modifier and Type | Field and Description |
|---|---|
static int |
SSH_FXF_APPEND
Force all writes to append data at the end of the file.
|
static int |
SSH_FXF_CREAT
If this flag is specified, then a new file will be created if one
does not alread exist (if O_TRUNC is specified, the new file will
be truncated to zero length if it previously exists).
|
static int |
SSH_FXF_EXCL
Causes the request to fail if the named file already exists.
|
static int |
SSH_FXF_READ
Open the file for reading.
|
static int |
SSH_FXF_TRUNC
Forces an existing file with the same name to be truncated to zero
length when creating a file by specifying SSH_FXF_CREAT.
|
static int |
SSH_FXF_WRITE
Open the file for writing.
|
| Constructor and Description |
|---|
SFTPv3Client(Connection conn)
Create a SFTP v3 client.
|
SFTPv3Client(Connection conn,
PacketListener listener)
Create a SFTP v3 client.
|
| Modifier and Type | Method and Description |
|---|---|
String |
canonicalPath(String path)
Have the server canonicalize any given path name to an absolute path.
|
void |
close()
Close this SFTP session.
|
void |
closeFile(SFTPv3FileHandle handle)
Close a file.
|
SFTPv3FileHandle |
createFile(String fileName)
Create a file and open it for reading and writing.
|
SFTPv3FileHandle |
createFile(String fileName,
SFTPv3FileAttributes attr)
Create a file and open it for reading and writing.
|
SFTPv3FileHandle |
createFileTruncate(String fileName)
Create a file (truncate it if it already exists) and open it for writing.
|
SFTPv3FileHandle |
createFileTruncate(String fileName,
SFTPv3FileAttributes attr)
reate a file (truncate it if it already exists) and open it for writing.
|
void |
createSymlink(String src,
String target)
Create a symbolic link on the server.
|
void |
fsetstat(SFTPv3FileHandle handle,
SFTPv3FileAttributes attr)
Modify the attributes of a file.
|
SFTPv3FileAttributes |
fstat(SFTPv3FileHandle handle)
Retrieve the file attributes of an open file.
|
String |
getCharset()
The currently used charset for filename encoding/decoding.
|
int |
getProtocolVersion()
Returns the negotiated SFTP protocol version between the client and the server.
|
boolean |
isConnected()
Queries the channel state
|
List<SFTPv3DirectoryEntry> |
ls(String dirName)
List the contents of a directory.
|
SFTPv3FileAttributes |
lstat(String path)
Retrieve the file attributes of a file.
|
void |
mkdir(String dirName,
int posixPermissions)
Create a new directory.
|
void |
mv(String oldPath,
String newPath)
Move a file or directory.
|
SFTPv3FileHandle |
openDirectory(String path) |
SFTPv3FileHandle |
openFile(String fileName,
int flags,
SFTPv3FileAttributes attr) |
SFTPv3FileHandle |
openFileRO(String fileName)
Open a file for reading.
|
SFTPv3FileHandle |
openFileRW(String fileName)
Open a file for reading and writing.
|
SFTPv3FileHandle |
openFileRWAppend(String fileName)
Open a file in append mode.
|
SFTPv3FileHandle |
openFileWAppend(String fileName)
Open a file in append mode.
|
int |
read(SFTPv3FileHandle handle,
long fileOffset,
byte[] dst,
int dstoff,
int len)
Read bytes from a file in a parallel fashion.
|
String |
readLink(String path)
Read the target of a symbolic link.
|
void |
rm(String fileName)
Remove a file.
|
void |
rmdir(String dirName)
Remove an empty directory.
|
void |
setCharset(String charset)
Set the charset used to convert between Java Unicode Strings and byte encodings
used by the server for paths and file names.
|
void |
setRequestParallelism(int parallelism) |
void |
setstat(String path,
SFTPv3FileAttributes attr)
Modify the attributes of a file.
|
SFTPv3FileAttributes |
stat(String path)
Retrieve the file attributes of a file.
|
void |
write(SFTPv3FileHandle handle,
long fileOffset,
byte[] src,
int srcoff,
int len)
Write bytes to a file.
|
public static final int SSH_FXF_READ
public static final int SSH_FXF_WRITE
public static final int SSH_FXF_APPEND
public static final int SSH_FXF_CREAT
public static final int SSH_FXF_TRUNC
public static final int SSH_FXF_EXCL
public SFTPv3Client(Connection conn, PacketListener listener) throws IOException
conn - The underlying SSH-2 connection to be used.IOExceptionpublic SFTPv3Client(Connection conn) throws IOException
conn - The underlying SSH-2 connection to be used.IOExceptionpublic void setCharset(String charset) throws IOException
charset - the name of the charset to be used or null to use the platform's
default encoding.IOExceptiongetCharset()public String getCharset()
null if the platform's default charset is being used)setCharset(String)public SFTPv3FileAttributes fstat(SFTPv3FileHandle handle) throws IOException
handle - a SFTPv3FileHandle handle.IOExceptionpublic SFTPv3FileAttributes stat(String path) throws IOException
path - See the comment for the class for more details.IOExceptionlstat(String)public SFTPv3FileAttributes lstat(String path) throws IOException
path - See the comment for the class for more details.IOExceptionstat(String)public String readLink(String path) throws IOException
lstat(String).path - See the comment for the class for more details.IOExceptionpublic void setstat(String path, SFTPv3FileAttributes attr) throws IOException
path - See the comment for the class for more details.attr - A SFTPv3FileAttributes object. Specifies the modifications to be
made to the attributes of the file. Empty fields will be ignored.IOExceptionpublic void fsetstat(SFTPv3FileHandle handle, SFTPv3FileAttributes attr) throws IOException
handle - a SFTPv3FileHandle handleattr - A SFTPv3FileAttributes object. Specifies the modifications to be
made to the attributes of the file. Empty fields will be ignored.IOExceptionpublic void createSymlink(String src, String target) throws IOException
src - See the comment for the class for more details.target - See the comment for the class for more details.IOExceptionpublic String canonicalPath(String path) throws IOException
path - See the comment for the class for more details.IOExceptionpublic final SFTPv3FileHandle openDirectory(String path) throws IOException
IOExceptionpublic int getProtocolVersion()
public boolean isConnected()
public void close()
close() method, you are likely wasting resources.public List<SFTPv3DirectoryEntry> ls(String dirName) throws IOException
dirName - See the comment for the class for more details.SFTPv3DirectoryEntry objects.IOExceptionpublic void mkdir(String dirName, int posixPermissions) throws IOException
dirName - See the comment for the class for more details.posixPermissions - the permissions for this directory, e.g., "0700" (remember that
this is octal noation). The server will likely apply a umask.IOExceptionpublic void rm(String fileName) throws IOException
fileName - See the comment for the class for more details.IOExceptionpublic void rmdir(String dirName) throws IOException
dirName - See the comment for the class for more details.IOExceptionpublic void mv(String oldPath, String newPath) throws IOException
oldPath - See the comment for the class for more details.newPath - See the comment for the class for more details.IOExceptionpublic SFTPv3FileHandle openFileRO(String fileName) throws IOException
fileName - See the comment for the class for more details.IOExceptionpublic SFTPv3FileHandle openFileRW(String fileName) throws IOException
fileName - See the comment for the class for more details.IOExceptionpublic SFTPv3FileHandle openFileRWAppend(String fileName) throws IOException
fileName - See the comment for the class for more details.IOExceptionpublic SFTPv3FileHandle openFileWAppend(String fileName) throws IOException
fileName - See the comment for the class for more details.IOExceptionpublic SFTPv3FileHandle createFile(String fileName) throws IOException
createFile(fileName, null).fileName - See the comment for the class for more details.IOExceptionpublic SFTPv3FileHandle createFile(String fileName, SFTPv3FileAttributes attr) throws IOException
fileName - See the comment for the class for more details.attr - may be null to use server defaults. Probably only
the uid, gid and permissions
(remember the server may apply a umask) entries of the SFTPv3FileHandle
structure make sense. You need only to set those fields where you want
to override the server's defaults.IOExceptionpublic SFTPv3FileHandle createFileTruncate(String fileName) throws IOException
createFileTruncate(fileName, null).fileName - See the comment for the class for more details.IOExceptionpublic SFTPv3FileHandle createFileTruncate(String fileName, SFTPv3FileAttributes attr) throws IOException
fileName - See the comment for the class for more details.attr - may be null to use server defaults. Probably only
the uid, gid and permissions
(remember the server may apply a umask) entries of the SFTPv3FileHandle
structure make sense. You need only to set those fields where you want
to override the server's defaults.IOExceptionpublic SFTPv3FileHandle openFile(String fileName, int flags, SFTPv3FileAttributes attr) throws IOException
IOExceptionpublic void setRequestParallelism(int parallelism)
parallelism - public int read(SFTPv3FileHandle handle, long fileOffset, byte[] dst, int dstoff, int len) throws IOException
len),
and return them.-1 is returned.
handle - a SFTPv3FileHandle handlefileOffset - offset (in bytes) in the filedst - the destination byte arraydstoff - offset in the destination byte arraylen - how many bytes to read, 0 < lenEOFIOExceptionpublic void write(SFTPv3FileHandle handle, long fileOffset, byte[] src, int srcoff, int len) throws IOException
len > 32768, then the write operation will
be split into multiple writes.handle - a SFTPv3FileHandle handle.fileOffset - offset (in bytes) in the file.src - the source byte array.srcoff - offset in the source byte array.len - how many bytes to write.IOExceptionpublic void closeFile(SFTPv3FileHandle handle) throws IOException
handle - a SFTPv3FileHandle handleIOExceptionCopyright © 2014. All Rights Reserved.