Package org.kiwiproject.jsch
Class SftpTransfers
- java.lang.Object
-
- org.kiwiproject.jsch.SftpTransfers
-
public class SftpTransfers extends Object
A simple wrapper around aJSch
instance that handles some basic SFTP operations.- Implementation Note:
- This requires JSch being available at runtime.
-
-
Constructor Summary
Constructors Constructor Description SftpTransfers(SftpConnector connector)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connect()
Connects to the remote SFTP server.void
deleteRemoteFile(Path remotePath, String remoteFilename)
Deletes a given file from the given path on a remote server.void
disconnect()
Disconnects from the remote SFTP server.void
getAndStoreAllFiles(Path remotePath, BiFunction<Path,String,Path> localPathSupplier, BiFunction<Path,String,String> localFilenameSupplier)
Recursively gets files off of a remote server starting in the given path and stores the files locally in the given path and given filename.void
getAndStoreFile(Path remotePath, Path localPath, String filename)
Gets a file off of a remote server in the given path and with the given filename and stores the file locally in a given path and the original (remote) filename.void
getAndStoreFile(Path remotePath, Path localPath, String remoteFilename, String localFilename)
Gets a file off of a remote server in the given path and with the given filename and stores the file locally in a given path and the given filename.void
getAndStoreFile(Path remotePath, BiFunction<Path,String,Path> localPathSupplier, String remoteFilename, BiFunction<Path,String,String> localFilenameSupplier)
Gets a file off of a remote server in the given path and with the given filename and stores the file locally in a given path and the given filename.String
getFileContent(Path remotePath, String remoteFilename)
Gets a file off of a remote server with the given path and given filename and returns the contents of the file as aString
.List<String>
listDirectories(Path remotePath)
Returns a list of directories that exist in the given path on the remote server.List<String>
listFiles(Path remotePath)
Returns a list of files that exist in the given path on the remote server.void
putFile(Path remotePath, String filename, InputStream data)
Pushes a stream of data to a remote SFTP server in the given path and with the given filename.
-
-
-
Constructor Detail
-
SftpTransfers
public SftpTransfers(SftpConnector connector)
-
-
Method Detail
-
connect
public void connect()
Connects to the remote SFTP server.- API Note:
- This is a convenience method that delegates to the internal
SftpConnector.connect()
-
disconnect
public void disconnect()
Disconnects from the remote SFTP server.- API Note:
- This is a convenience method that delegates to the internal
SftpConnector.disconnect()
-
putFile
public void putFile(Path remotePath, String filename, InputStream data)
Pushes a stream of data to a remote SFTP server in the given path and with the given filename.- Parameters:
remotePath
- the path on the remote server where the file will be createdfilename
- the filename to give the file on the remote serverdata
- the stream of data to write to the remote server
-
getAndStoreAllFiles
public void getAndStoreAllFiles(Path remotePath, BiFunction<Path,String,Path> localPathSupplier, BiFunction<Path,String,String> localFilenameSupplier)
Recursively gets files off of a remote server starting in the given path and stores the files locally in the given path and given filename. The local path will be determined through the givenBiFunction
supplier which is provided the current remote path and current remote filename. The local filename will be determined through the givenBiFunction
supplier which is provided the remote path and remote filename.- Parameters:
remotePath
- path on the remote server where the file is locatedlocalPathSupplier
- supplier that calculates the path on the local machine where the file will be writtenlocalFilenameSupplier
- supplier that calculates the name of the file that will be written locally
-
getAndStoreFile
public void getAndStoreFile(Path remotePath, Path localPath, String filename)
Gets a file off of a remote server in the given path and with the given filename and stores the file locally in a given path and the original (remote) filename.- Parameters:
remotePath
- path on the remote server where the file is locatedlocalPath
- path on the local machine where the file will be writtenfilename
- name of the file to pull from the remote server (This name is used as the local file name)
-
getAndStoreFile
public void getAndStoreFile(Path remotePath, Path localPath, String remoteFilename, String localFilename)
Gets a file off of a remote server in the given path and with the given filename and stores the file locally in a given path and the given filename.- Parameters:
remotePath
- path on the remote server where the file is locatedlocalPath
- path on the local machine where the file will be writtenremoteFilename
- name of the file to pull from the remote serverlocalFilename
- name of the file that will be written locally
-
getAndStoreFile
public void getAndStoreFile(Path remotePath, BiFunction<Path,String,Path> localPathSupplier, String remoteFilename, BiFunction<Path,String,String> localFilenameSupplier)
Gets a file off of a remote server in the given path and with the given filename and stores the file locally in a given path and the given filename. The local path will be determined through the givenBiFunction
supplier which is provided the remote path and remote filename. The local filename will be determined through the givenBiFunction
supplier which is provided the remote path and remote filename.- Parameters:
remotePath
- path on the remote server where the file is locatedlocalPathSupplier
- supplier that calculates the path on the local machine where the file will be writtenremoteFilename
- name of the file to pull from the remote serverlocalFilenameSupplier
- supplier that calculates the name of the file that will be written locally
-
getFileContent
public String getFileContent(Path remotePath, String remoteFilename)
Gets a file off of a remote server with the given path and given filename and returns the contents of the file as aString
.- Parameters:
remotePath
- path on the remote server where the file is locatedremoteFilename
- name of the file to pull from the remote server- Returns:
- contents of the retrieved file as a
String
-
listFiles
public List<String> listFiles(Path remotePath)
Returns a list of files that exist in the given path on the remote server.- Parameters:
remotePath
- path on the remote server to list files- Returns:
- a list of filenames that exist in the given path
-
listDirectories
public List<String> listDirectories(Path remotePath)
Returns a list of directories that exist in the given path on the remote server.- Parameters:
remotePath
- path on the remote server to list directories- Returns:
- a list of directories that exist in the given path
-
-