Package org.kiwiproject.jsch
Class SftpTransfers
- java.lang.Object
-
- org.kiwiproject.jsch.SftpTransfers
-
public class SftpTransfers extends Object
A simple wrapper around aJSchinstance 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 voidconnect()Connects to the remote SFTP server.voiddeleteRemoteFile(Path remotePath, String remoteFilename)Deletes a given file from the given path on a remote server.voiddisconnect()Disconnects from the remote SFTP server.voidgetAndStoreAllFiles(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.voidgetAndStoreFile(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.voidgetAndStoreFile(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.voidgetAndStoreFile(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.StringgetFileContent(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.InputStreamgetFileContentAsInputStream(Path remotePath, String remoteFilename)Gets a file off of a remote server with the given path and given filename and returns anInputStream.List<String>listDirectories(Path remotePath)Returns a list of directories that exist in the given path on the remote server.List<String>listDirectories(Path remotePath, Predicate<String> dirFilter)Returns a list of directories that exist in the given path and matching the given directory filter on the remote server.List<String>listFiles(Path remotePath)Returns a list of files that exist in the given path on the remote server.List<String>listFiles(Path remotePath, Predicate<String> fileFilter)Returns a list of files that exist in the given path and matching the given file filter on the remote server.voidputFile(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 givenBiFunctionsupplier which is provided the current remote path and current remote filename. The local filename will be determined through the givenBiFunctionsupplier 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 givenBiFunctionsupplier which is provided the remote path and remote filename. The local filename will be determined through the givenBiFunctionsupplier 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
-
getFileContentAsInputStream
public InputStream getFileContentAsInputStream(Path remotePath, String remoteFilename)
Gets a file off of a remote server with the given path and given filename and returns anInputStream. This is useful if the remote file is binary and not a text based file.Note: The caller of this method is responsible for closing the stream.
- Parameters:
remotePath- path on the remote server where the file is locatedremoteFilename- name of the file to pull from the remote server- Returns:
- an
InputStreamto read the file
-
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
-
listFiles
public List<String> listFiles(Path remotePath, Predicate<String> fileFilter)
Returns a list of files that exist in the given path and matching the given file filter on the remote server.- Parameters:
remotePath- path on the remote server to list filesfileFilter- predicate to filter file names being returned- Returns:
- a list of filenames that exist in the given path matching the given file filter
-
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
-
listDirectories
public List<String> listDirectories(Path remotePath, Predicate<String> dirFilter)
Returns a list of directories that exist in the given path and matching the given directory filter on the remote server.- Parameters:
remotePath- path on the remote server to list filesdirFilter- predicate to filter directory names being returned- Returns:
- a list of directories that exist in the given path matching the given directory filter
-
-