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:
If you are still not tired then please go on and read the comment for
setCharset(String).
| Constructor and Description |
|---|
SFTPv3Client(Connection conn)
Create a SFTP v3 client.
|
SFTPv3Client(Connection conn,
PrintStream debug)
Deprecated.
this constructor (debug version) will disappear in the future,
use
SFTPv3Client(Connection) instead. |
| Modifier and Type | Method and Description |
|---|---|
SFTPv3FileAttributes |
_stat(String path)
Graceful
stat(String) that returns null if the path doesn't exist. |
String |
canonicalPath(String path)
Have the server canonicalize any given path name to an absolute path.
|
void |
chmod(String path,
int permissions) |
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 reading and writing.
|
SFTPv3FileHandle |
createFileTruncate(String fileName,
SFTPv3FileAttributes attr)
reate a file (truncate it if it already exists) and open it for reading and writing.
|
void |
createSymlink(String src,
String target)
Create a symbolic link on the server.
|
boolean |
exists(String path)
Checks if the given path exists.
|
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.
|
Vector |
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 |
mkdirs(String path,
int posixPermission)
Makes sure that the directory exists, by creating it if necessary.
|
void |
mv(String oldPath,
String newPath)
Move a file or directory.
|
SFTPv3FileHandle |
openFileRO(String fileName)
Open a file for reading.
|
SFTPv3FileHandle |
openFileRW(String fileName)
Open a file for reading and writing.
|
int |
read(SFTPv3FileHandle handle,
long fileOffset,
byte[] dst,
int dstoff,
int len)
Read bytes from a file.
|
InputStream |
read(String file) |
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 |
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.
|
OutputStream |
writeToFile(String path)
Creates a new file and writes to it.
|
public SFTPv3Client(Connection conn, PrintStream debug) throws IOException
SFTPv3Client(Connection) instead.conn - The underlying SSH-2 connection to be used.debug - IOExceptionpublic SFTPv3Client(Connection conn) throws IOException
conn - The underlying SSH-2 connection to be used.IOExceptionpublic void setCharset(String charset) throws IOException
If you don't set anything, then the platform default will be used (this is the default behavior).
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
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 int getProtocolVersion()
public void close()
close() method, you are likely wasting resources.public Vector 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 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 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 < len <= 32768 bytesEOFIOExceptionpublic 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 handleIOExceptionpublic boolean exists(String path) throws IOException
IOExceptionpublic SFTPv3FileAttributes _stat(String path) throws IOException
stat(String) that returns null if the path doesn't exist.IOExceptionpublic void mkdirs(String path, int posixPermission) throws IOException
IOExceptionpublic OutputStream writeToFile(String path) throws IOException
IOExceptionpublic InputStream read(String file) throws IOException
IOExceptionpublic void chmod(String path, int permissions) throws IOException
IOExceptionCopyright © 2014. All rights reserved.