public class SftpSubsystemChannel extends SubsystemChannel
// Create an SshClient SshConnector con = SshConnector.getInstance(); // Connect and authenticate an SshClient SshClient ssh = con.connect(....); .... // Create and initialize an SftpSubsystemChannel SshSession session = ssh.openSessionChannel(); if(session instanceof Ssh2Session) ((Ssh2Session)session).startSubsystem("sftp"); SftpSubsystemChannel sftp = new SftpSubsystemChannel(session); sftp.initialize();
Modifier and Type | Field and Description |
---|---|
static int |
MAX_VERSION |
static int |
OPEN_APPEND
File open flag, forces all writes to append data at the end of the file.
|
static int |
OPEN_CREATE
File open flag, if specified a new file will be created if one does not
already exist.
|
static int |
OPEN_EXCLUSIVE
File open flag, causes an open request to fail if the named file already
exists.
|
static int |
OPEN_READ
File open flag, opens the file for reading.
|
static int |
OPEN_TEXT
File open flag, causes the file to be opened in text mode.
|
static int |
OPEN_TRUNCATE
File open flag, forces an existing file with the same name to be
truncated to zero length when creating a file by specifying OPEN_CREATE.
|
static int |
OPEN_WRITE
File open flag, opens the file for writing.
|
channel
Constructor and Description |
---|
SftpSubsystemChannel(SshSession session) |
SftpSubsystemChannel(SshSession session,
int Max_Version) |
Modifier and Type | Method and Description |
---|---|
void |
changePermissions(SftpFile file,
int permissions)
Change the permissions of a file.
|
void |
changePermissions(String filename,
int permissions)
Change the permissions of a file.
|
void |
changePermissions(String filename,
String permissions)
Change the permissions of a file.
|
void |
close()
Close the subsystem
|
void |
closeFile(SftpFile file)
Close a file or directory.
|
void |
createSymbolicLink(String targetpath,
String linkpath)
Create a symbolic link.
|
String |
getAbsolutePath(SftpFile file)
Get the absolute path of a file.
|
String |
getAbsolutePath(String path)
Get the absolute path of a file.
|
SftpFileAttributes |
getAttributes(SftpFile file)
Get the attributes of a file.
|
SftpFileAttributes |
getAttributes(String path)
Get the attributes of a file.
|
protected SftpFileAttributes |
getAttributes(String path,
int messageId) |
byte[] |
getCanonicalNewline()
Returns the canonical newline convention in use when reading/writing text
files.
|
String |
getCharsetEncoding()
Get the current encoding being used for filename Strings.
|
String |
getDefaultDirectory()
Gets the users default directory.
|
byte[] |
getExtension(String name)
Get the data value of a supported SFTP extension.
|
SftpFile |
getFile(String path)
Utility method to obtain an
SftpFile instance for a given path. |
SftpFileAttributes |
getLinkAttributes(String path)
Get the attributes of a file.
|
void |
getOKRequestStatus(UnsignedInteger32 requestId)
Verify that an OK status has been returned for a request id.
|
int |
getServerVersion()
Version 4 of the SFTP protocol allows the server to return its maximum
supported version instead of the actual version to be used.
|
String |
getSymbolicLinkTarget(String linkpath)
Get the target path of a symbolic link.
|
int |
getVersion()
When called after the initialize method this
will return the version in operation for this sftp session.
|
void |
initialize()
Initializes the sftp subsystem and negotiates a version with the server.
|
int |
listChildren(SftpFile file,
Vector<SftpFile> children)
List the children of a directory.
|
void |
makeDirectory(String path)
Make a directory.
|
void |
makeDirectory(String path,
SftpFileAttributes attrs)
Make a directory.
|
SftpFile |
openDirectory(String path)
Open a directory.
|
SftpFile |
openFile(String absolutePath,
int flags)
Open a file.
|
SftpFile |
openFile(String absolutePath,
int flags,
SftpFileAttributes attrs)
Open a file.
|
void |
performOptimizedRead(byte[] handle,
long length,
int blocksize,
OutputStream out,
int outstandingRequests,
FileTransferProgress progress)
Performs an optimized read of a file through use of asynchronous
messages.
|
void |
performOptimizedRead(byte[] handle,
long length,
int blocksize,
OutputStream out,
int outstandingRequests,
FileTransferProgress progress,
long position)
Performs an optimized read of a file through use of asynchronous
messages.
|
void |
performOptimizedWrite(byte[] handle,
int blocksize,
int outstandingRequests,
InputStream in,
int buffersize,
FileTransferProgress progress)
Performs an optimized write of a file through asynchronous messaging and
through buffering the local file into memory.
|
void |
performOptimizedWrite(byte[] handle,
int blocksize,
int outstandingRequests,
InputStream in,
int buffersize,
FileTransferProgress progress,
long position)
Performs an optimized write of a file through asynchronous messaging and
through buffering the local file into memory.
|
void |
performSynchronousRead(byte[] handle,
int blocksize,
OutputStream out,
FileTransferProgress progress,
long position)
Perform a synchronous read of a file from the remote file system.
|
UnsignedInteger32 |
postReadRequest(byte[] handle,
long offset,
int len)
Post a read request to the server and return the request id; this is used
to optimize file downloads.
|
UnsignedInteger32 |
postWriteRequest(byte[] handle,
long position,
byte[] data,
int off,
int len)
Send a write request for an open file but do not wait for the response
from the server.
|
int |
readFile(byte[] handle,
UnsignedInteger64 offset,
byte[] output,
int off,
int len)
Read a block of data from an open file.
|
void |
recurseMakeDirectory(String path)
Recurse through a hierarchy of directories creating them as necessary.
|
void |
removeDirectory(String path)
Remove an empty directory.
|
void |
removeFile(String filename)
Remove a file.
|
void |
renameFile(String oldpath,
String newpath)
Rename an existing file.
|
SftpMessage |
sendExtensionMessage(String request,
byte[] requestData)
Send an extension message and return the response.
|
void |
setAttributes(SftpFile file,
SftpFileAttributes attrs)
Sets the attributes of a file.
|
void |
setAttributes(String path,
SftpFileAttributes attrs)
Sets the attributes of a file.
|
void |
setCharsetEncoding(String charset)
Allows the default character encoding to be overriden for filename
strings.
|
static void |
setMaxSftpVersion(int MAX_VERSION)
Sets the maximum SFTP protocol version to use, this should be <=4.
|
void |
setThisMaxSftpVersion(int MAX_VERSION)
Sets the maximum SFTP protocol version to use for this instance, this
should be <=4.
|
boolean |
supportsExtension(String name)
Does the server support an SFTP extension? This checks the extensions
returned by the server during the SFTP version negotiation.
|
void |
writeFile(byte[] handle,
UnsignedInteger64 offset,
byte[] data,
int off,
int len)
Write a block of data to an open file.
|
createPacket, isClosed, nextMessage, sendMessage, sendMessage
public static final int OPEN_READ
public static final int OPEN_WRITE
public static final int OPEN_APPEND
public static final int OPEN_CREATE
public static final int OPEN_TRUNCATE
public static final int OPEN_EXCLUSIVE
public static final int OPEN_TEXT
public static int MAX_VERSION
public SftpSubsystemChannel(SshSession session) throws SshException
SshException
public SftpSubsystemChannel(SshSession session, int Max_Version) throws SshException
SshException
public static void setMaxSftpVersion(int MAX_VERSION)
MAX_VERSION
- public void setThisMaxSftpVersion(int MAX_VERSION)
MAX_VERSION
- public int getVersion()
public byte[] getCanonicalNewline() throws SftpStatusException
SftpStatusException
public void initialize() throws SshException, UnsupportedEncodingException
public void close() throws IOException
SubsystemChannel
close
in class SubsystemChannel
IOException
public void setCharsetEncoding(String charset) throws SshException, UnsupportedEncodingException
charset
- UnsupportedEncodingException
SshException
public int getServerVersion()
public String getCharsetEncoding()
public boolean supportsExtension(String name)
name
- Stringpublic byte[] getExtension(String name)
supportsExtension(String)
before calling this method to determine if the
extension is available.name
- Stringpublic SftpMessage sendExtensionMessage(String request, byte[] requestData) throws SshException, SftpStatusException
request
- StringrequestData
- byte[]SshException
SftpStatusException
public void changePermissions(SftpFile file, int permissions) throws SftpStatusException, SshException
file
- the filepermissions
- an integer value containing a file permissions maskSshException
- ,SftpStatusExceptionSftpStatusException
public void changePermissions(String filename, int permissions) throws SftpStatusException, SshException
filename
- the path to the file.permissions
- an integer value containing a file permissions mask.SshException
- ,SftpStatusExceptionSftpStatusException
public void changePermissions(String filename, String permissions) throws SftpStatusException, SshException
filename
- the path to the file.permissions
- a string containing the permissions, for example "rw-r--r--"SftpStatusException
- , SshExceptionSshException
public void setAttributes(String path, SftpFileAttributes attrs) throws SftpStatusException, SshException
path
- the path to the file.attrs
- the file attributes.SftpStatusException
- , SshExceptionSshException
public void setAttributes(SftpFile file, SftpFileAttributes attrs) throws SftpStatusException, SshException
file
- the file object.attrs
- the new attributes.SshException
SftpStatusException
public UnsignedInteger32 postWriteRequest(byte[] handle, long position, byte[] data, int off, int len) throws SftpStatusException, SshException
handle
- position
- data
- off
- len
- SshException
SftpStatusException
public void writeFile(byte[] handle, UnsignedInteger64 offset, byte[] data, int off, int len) throws SftpStatusException, SshException
handle
- the open file handle.offset
- the offset in the file to start writingdata
- a buffer containing the data to writeoff
- the offset to start in the bufferlen
- the length of data to write (setting to false will increase
file transfer but may miss errors)SshException
SftpStatusException
public void performOptimizedWrite(byte[] handle, int blocksize, int outstandingRequests, InputStream in, int buffersize, FileTransferProgress progress) throws SftpStatusException, SshException, TransferCancelledException
handle
- the open file handle to write toblocksize
- the block size to send data, should be between 4096 and 65535outstandingRequests
- the maximum number of requests that can be outstanding at any
one timein
- the InputStream to read frombuffersize
- the size of the temporary buffer to read from the InputStream.
Data is buffered into a temporary buffer so that the number of
local filesystem reads is reducted to a minimum. This
increases performance and so the buffer size should be as high
as possible. The default operation, if buffersize <= 0 is to
allocate a buffer the same size as the blocksize, meaning no
buffer optimization is performed.progress
- provides progress information, may be null.SshException
SftpStatusException
TransferCancelledException
public void performOptimizedWrite(byte[] handle, int blocksize, int outstandingRequests, InputStream in, int buffersize, FileTransferProgress progress, long position) throws SftpStatusException, SshException, TransferCancelledException
handle
- the open file handle to write toblocksize
- the block size to send data, should be between 4096 and 65535outstandingRequests
- the maximum number of requests that can be outstanding at any
one timein
- the InputStream to read frombuffersize
- the size of the temporary buffer to read from the InputStream.
Data is buffered into a temporary buffer so that the number of
local filesystem reads is reducted to a minimum. This
increases performance and so the buffer size should be as high
as possible. The default operation, if buffersize <= 0 is to
allocate a buffer the same size as the blocksize, meaning no
buffer optimization is performed.progress
- provides progress information, may be null.position
- the position in the file to start writing to.SshException
SftpStatusException
TransferCancelledException
public void performOptimizedRead(byte[] handle, long length, int blocksize, OutputStream out, int outstandingRequests, FileTransferProgress progress) throws SftpStatusException, SshException, TransferCancelledException
handle
- the open files handlelength
- the length of the fileblocksize
- the blocksize to readout
- an OutputStream to output the file intooutstandingRequests
- the maximum number of read requests toprogress
- SshException
SftpStatusException
TransferCancelledException
public void performOptimizedRead(byte[] handle, long length, int blocksize, OutputStream out, int outstandingRequests, FileTransferProgress progress, long position) throws SftpStatusException, SshException, TransferCancelledException
handle
- the open files handlelength
- the amount of the file file to be read, equal to the file
length when reading the whole fileblocksize
- the blocksize to readout
- an OutputStream to output the file intooutstandingRequests
- the maximum number of read requests toprogress
- position
- the postition from which to start reading the fileSshException
SftpStatusException
TransferCancelledException
public void performSynchronousRead(byte[] handle, int blocksize, OutputStream out, FileTransferProgress progress, long position) throws SftpStatusException, SshException, TransferCancelledException
handle
- blocksize
- out
- progress
- position
- SftpStatusException
SshException
TransferCancelledException
public UnsignedInteger32 postReadRequest(byte[] handle, long offset, int len) throws SftpStatusException, SshException
handle
- offset
- len
- SshException
SftpStatusException
public int readFile(byte[] handle, UnsignedInteger64 offset, byte[] output, int off, int len) throws SftpStatusException, SshException
handle
- the open file handleoffset
- the offset to start reading in the fileoutput
- a buffer to write the returned data tooff
- the starting offset in the output bufferlen
- the length of data to readSshException
SftpStatusException
public SftpFile getFile(String path) throws SftpStatusException, SshException
SftpFile
instance for a given path.path
- SftpStatusException
SshException
public String getAbsolutePath(SftpFile file) throws SftpStatusException, SshException
file
- SshException
SftpStatusException
public void createSymbolicLink(String targetpath, String linkpath) throws SftpStatusException, SshException
targetpath
- the symbolic link to createlinkpath
- the path to which the symbolic link pointsSshException
- if the remote SFTP version is < 3 an exception is thrown as
this feature is not supported by previous versions of the
protocol.SftpStatusException
public String getSymbolicLinkTarget(String linkpath) throws SftpStatusException, SshException
linkpath
- SshException
- if the remote SFTP version is < 3 an exception is thrown as
this feature is not supported by previous versions of the
protocol.SftpStatusException
public String getDefaultDirectory() throws SftpStatusException, SshException
SshException
SftpStatusException
public String getAbsolutePath(String path) throws SftpStatusException, SshException
path
- SshException
SftpStatusException
public int listChildren(SftpFile file, Vector<SftpFile> children) throws SftpStatusException, SshException
List the children of a directory.
To use this method first open a directory with the openDirectory method and then create a Vector to store the results. To retrieve the results keep calling this method until it returns -1 which indicates no more results will be returned.
SftpFile dir = sftp.openDirectory("code/foobar"); Vector results = new Vector(); while (sftp.listChildren(dir, results) > -1) ; sftp.closeFile(dir);
file
- children
- SftpStatusException
- , SshExceptionSshException
public void recurseMakeDirectory(String path) throws SftpStatusException, SshException
path
- SftpStatusException
- , SshExceptionSshException
public SftpFile openFile(String absolutePath, int flags) throws SftpStatusException, SshException
absolutePath
- flags
- SftpStatusException
- , SshExceptionSshException
public SftpFile openFile(String absolutePath, int flags, SftpFileAttributes attrs) throws SftpStatusException, SshException
absolutePath
- flags
- attrs
- SftpStatusException
- , SshExceptionSshException
public SftpFile openDirectory(String path) throws SftpStatusException, SshException
path
- SftpStatusException
- , SshExceptionSshException
public void closeFile(SftpFile file) throws SftpStatusException, SshException
file
- SftpStatusException
- , SshExceptionSshException
public void removeDirectory(String path) throws SftpStatusException, SshException
path
- SftpStatusException
- , SshExceptionSshException
public void removeFile(String filename) throws SftpStatusException, SshException
filename
- SftpStatusException
- , SshExceptionSshException
public void renameFile(String oldpath, String newpath) throws SftpStatusException, SshException
oldpath
- newpath
- SftpStatusException
- , SshExceptionSshException
public SftpFileAttributes getAttributes(String path) throws SftpStatusException, SshException
path
- SshException
SftpStatusException
public SftpFileAttributes getLinkAttributes(String path) throws SftpStatusException, SshException
path
- SftpStatusException
SshException
protected SftpFileAttributes getAttributes(String path, int messageId) throws SftpStatusException, SshException
SftpStatusException
SshException
public SftpFileAttributes getAttributes(SftpFile file) throws SftpStatusException, SshException
file
- SftpStatusException
- , SshExceptionSshException
public void makeDirectory(String path) throws SftpStatusException, SshException
path
- SftpStatusException
- , SshExceptionSshException
public void makeDirectory(String path, SftpFileAttributes attrs) throws SftpStatusException, SshException
path
- attrs
- SftpStatusException
- , SshExceptionSshException
public void getOKRequestStatus(UnsignedInteger32 requestId) throws SftpStatusException, SshException
requestId
- SftpStatusException
- , SshExceptionSshException
Copyright © 2014. All rights reserved.