Class SftpTransfers

java.lang.Object
org.kiwiproject.jsch.SftpTransfers

public class SftpTransfers extends Object
A simple wrapper around a JSch instance that handles some basic SFTP operations.
Implementation Note:
This requires JSch being available at runtime.
  • Constructor Details

  • Method Details

    • 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 created
      filename - the filename to give the file on the remote server
      data - 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 given BiFunction supplier which is provided the current remote path and current remote filename. The local filename will be determined through the given BiFunction supplier which is provided the remote path and remote filename.
      Parameters:
      remotePath - path on the remote server where the file is located
      localPathSupplier - supplier that calculates the path on the local machine where the file will be written
      localFilenameSupplier - 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 located
      localPath - path on the local machine where the file will be written
      filename - 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 located
      localPath - path on the local machine where the file will be written
      remoteFilename - name of the file to pull from the remote server
      localFilename - 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 given BiFunction supplier which is provided the remote path and remote filename. The local filename will be determined through the given BiFunction supplier which is provided the remote path and remote filename.
      Parameters:
      remotePath - path on the remote server where the file is located
      localPathSupplier - supplier that calculates the path on the local machine where the file will be written
      remoteFilename - name of the file to pull from the remote server
      localFilenameSupplier - 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 a String.
      Parameters:
      remotePath - path on the remote server where the file is located
      remoteFilename - 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 an InputStream. 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 located
      remoteFilename - name of the file to pull from the remote server
      Returns:
      an InputStream to 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 files
      fileFilter - 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 files
      dirFilter - predicate to filter directory names being returned
      Returns:
      a list of directories that exist in the given path matching the given directory filter
    • deleteRemoteFile

      public void deleteRemoteFile(Path remotePath, String remoteFilename)
      Deletes a given file from the given path on a remote server.
      Parameters:
      remotePath - path on the remote server where the file is located
      remoteFilename - name of the file to delete from the remote server