public class SftpSubsystemChannel extends SubsystemChannel
The above example demonstrates how to execute subsystem on both SSH1 and SSH2 connections. There is no subsystem support within SSH1 which requires that the command be executed directly.// 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"); else session.executeCommand("/usr/sbin/sftp-server"); 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.
|
boolean |
PROCESSING_PATHS |
static int |
SSH_FXF_ACCESS_APPEND_DATA |
static int |
SSH_FXF_ACCESS_APPEND_DATA_ATOMIC |
static int |
SSH_FXF_ACCESS_AUDIT_ALARM_INFO |
static int |
SSH_FXF_ACCESS_BACKUP |
static int |
SSH_FXF_ACCESS_BLOCK_ADVISORY |
static int |
SSH_FXF_ACCESS_BLOCK_DELETE |
static int |
SSH_FXF_ACCESS_BLOCK_READ |
static int |
SSH_FXF_ACCESS_BLOCK_WRITE |
static int |
SSH_FXF_ACCESS_DISPOSITION
Version 5 new flags
|
static int |
SSH_FXF_ACCESS_TEXT_MODE |
static int |
SSH_FXF_BACKUP_STREAM |
static int |
SSH_FXF_CREATE_NEW |
static int |
SSH_FXF_CREATE_TRUNCATE |
static int |
SSH_FXF_DELETE_ON_CLOSE |
static int |
SSH_FXF_NOFOLLOW |
static int |
SSH_FXF_OPEN_EXISTING |
static int |
SSH_FXF_OPEN_OR_CREATE |
static int |
SSH_FXF_OVERRIDE_OWNER |
static int |
SSH_FXF_TRUNCATE_EXISTING |
static int |
SSH_FXP_RENAME_ATOMIC |
static int |
SSH_FXP_RENAME_NATIVE |
static int |
SSH_FXP_RENAME_OVERWRITE |
channel
Constructor and Description |
---|
SftpSubsystemChannel(SshSession session) |
SftpSubsystemChannel(SshSession session,
int maximumVersion) |
SftpSubsystemChannel(SshSession session,
int maximumVersion,
int timeout) |
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 |
createLink(String targetpath,
String linkpath,
boolean symbolic)
Create a symbolic link.
|
void |
createSymbolicLink(String targetpath,
String linkpath)
Create a symbolic link.
|
void |
disableVerification() |
void |
enableVerification() |
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,
String messageName) |
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.
|
SftpMessage |
getExtensionResponse(UnsignedInteger32 requestId) |
SftpFile |
getFile(String path)
Utility method to obtain an
SftpFile instance for a given path. |
byte[] |
getHandleResponse(SftpMessage bar,
UnsignedInteger32 requestId) |
byte[] |
getHandleResponse(UnsignedInteger32 requestId) |
SftpFileAttributes |
getLinkAttributes(String path)
Get the attributes of a file.
|
int |
getMaximumLocalPacketLength() |
long |
getMaximumLocalWindowSize() |
int |
getMaximumRemotePacketLength() |
long |
getMaximumRemoteWindowSize() |
void |
getOKRequestStatus(UnsignedInteger32 requestId)
Verify that an OK status has been returned for a request id.
|
SftpMessage |
getResponse(UnsignedInteger32 requestId) |
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.
|
SftpFile |
getSingleFileResponse(SftpMessage bar,
String messageName)
Get a single SftpFile object from a message returning SSH_FXP_NAME result.
|
protected static String |
getStatusDescription(ByteArrayReader msg) |
String |
getSymbolicLinkTarget(String linkpath)
Get the target path of a symbolic link.
|
TransportProtocol |
getTransport() |
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.
|
int |
listChildren(SftpFile file,
Vector<SftpFile> children,
boolean disableAttributeCheck) |
void |
lockFile(byte[] handle,
long offset,
long length,
int lockFlags) |
void |
makeDirectory(String path)
Make a directory.
|
void |
makeDirectory(String path,
SftpFileAttributes attrs)
Make a directory.
|
SftpFile |
openDirectory(String path)
Open a directory.
|
SftpFile |
openDirectory(String path,
boolean disableAttributeCheck)
Open a directory.
|
SftpFile |
openFile(String absolutePath,
int flags)
Open a file.
|
SftpFile |
openFile(String absolutePath,
int flags,
SftpFileAttributes attrs)
Open a file.
|
SftpFile |
openFileVersion5(String absolutePath,
int flags,
int accessFlags,
SftpFileAttributes attrs) |
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.
|
protected void |
processSupported(byte[] data) |
protected void |
processSupported2(byte[] data) |
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.
|
void |
renameFile(String oldpath,
String newpath,
int flags) |
UnsignedInteger32 |
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.
|
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 |
unlockFile(byte[] handle,
long offset,
long length) |
void |
writeFile(byte[] handle,
UnsignedInteger64 offset,
byte[] data,
int off,
int len)
Write a block of data to an open file.
|
createPacket, debug, debug, isClosed, nextMessage, 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 final int MAX_VERSION
public static final int SSH_FXF_ACCESS_DISPOSITION
public static final int SSH_FXF_CREATE_NEW
public static final int SSH_FXF_CREATE_TRUNCATE
public static final int SSH_FXF_OPEN_EXISTING
public static final int SSH_FXF_OPEN_OR_CREATE
public static final int SSH_FXF_TRUNCATE_EXISTING
public static final int SSH_FXF_ACCESS_APPEND_DATA
public static final int SSH_FXF_ACCESS_APPEND_DATA_ATOMIC
public static final int SSH_FXF_ACCESS_TEXT_MODE
public static final int SSH_FXF_ACCESS_BLOCK_READ
public static final int SSH_FXF_ACCESS_BLOCK_WRITE
public static final int SSH_FXF_ACCESS_BLOCK_DELETE
public static final int SSH_FXF_ACCESS_BLOCK_ADVISORY
public static final int SSH_FXF_NOFOLLOW
public static final int SSH_FXF_DELETE_ON_CLOSE
public static final int SSH_FXF_ACCESS_AUDIT_ALARM_INFO
public static final int SSH_FXF_ACCESS_BACKUP
public static final int SSH_FXF_BACKUP_STREAM
public static final int SSH_FXF_OVERRIDE_OWNER
public static final int SSH_FXP_RENAME_OVERWRITE
public static final int SSH_FXP_RENAME_ATOMIC
public static final int SSH_FXP_RENAME_NATIVE
public final boolean PROCESSING_PATHS
public SftpSubsystemChannel(SshSession session) throws SshException
SshException
public SftpSubsystemChannel(SshSession session, int maximumVersion) throws SshException
SshException
public SftpSubsystemChannel(SshSession session, int maximumVersion, int timeout) throws SshException
SshException
public int getVersion()
public void enableVerification()
public void disableVerification()
public TransportProtocol getTransport()
public byte[] getCanonicalNewline() throws SftpStatusException
SftpStatusException
public void close() throws IOException
SubsystemChannel
close
in class SubsystemChannel
IOException
public void initialize() throws SshException, UnsupportedEncodingException
protected void processSupported2(byte[] data) throws IOException
IOException
protected void processSupported(byte[] data) throws IOException
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 UnsignedInteger32 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 65536outstandingRequests
- 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 65536outstandingRequests
- 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 lockFile(byte[] handle, long offset, long length, int lockFlags) throws SftpStatusException, SshException
SftpStatusException
SshException
public void unlockFile(byte[] handle, long offset, long length) throws SftpStatusException, SshException
SftpStatusException
SshException
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 void createLink(String targetpath, String linkpath, boolean symbolic) 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
protected static String getStatusDescription(ByteArrayReader msg) throws IOException
IOException
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 int listChildren(SftpFile file, Vector<SftpFile> children, boolean disableAttributeCheck) throws SftpStatusException, SshException
SftpStatusException
SshException
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 openFileVersion5(String absolutePath, int flags, int accessFlags, SftpFileAttributes attrs) throws SftpStatusException, SshException
SftpStatusException
SshException
public SftpFile openDirectory(String path) throws SftpStatusException, SshException
path
- SftpStatusException
- , SshExceptionSshException
public SftpFile openDirectory(String path, boolean disableAttributeCheck) throws SftpStatusException, SshException
path
- disableAttributeCheck
- disable checks on the attributes returned.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 void renameFile(String oldpath, String newpath, int flags) throws SftpStatusException, SshException
SftpStatusException
SshException
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, String messageName) 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 byte[] getHandleResponse(UnsignedInteger32 requestId) throws SftpStatusException, SshException
SftpStatusException
SshException
public byte[] getHandleResponse(SftpMessage bar, UnsignedInteger32 requestId) throws SftpStatusException, SshException
SftpStatusException
SshException
public SftpMessage getExtensionResponse(UnsignedInteger32 requestId) throws SftpStatusException, SshException
SftpStatusException
SshException
public void getOKRequestStatus(UnsignedInteger32 requestId) throws SftpStatusException, SshException
requestId
- SftpStatusException
- , SshExceptionSshException
public SftpMessage getResponse(UnsignedInteger32 requestId) throws SshException
SshException
public SftpFile getSingleFileResponse(SftpMessage bar, String messageName) throws SshException, SftpStatusException
bar
- messageName
- SshException
SftpStatusException
public int getMaximumRemotePacketLength()
public int getMaximumLocalPacketLength()
public long getMaximumLocalWindowSize()
public long getMaximumRemoteWindowSize()
Copyright © 2024. All rights reserved.