Class NSFTPClient

java.lang.Object
com.netlang.ftp.NSFTPClient
All Implemented Interfaces:
IFTPClient, com.netlang.io.filesystem.FileSystem

public class NSFTPClient extends Object implements IFTPClient
This class communicates with FTP servers with the SSH File Transfer protocol.
The class provides file access, file transfer, and file management functionalities over any reliable data stream.

  	 	IFTPClient sftpClient1 =  new NSFTPClient("myftpserver.com",22,"username","password",null);
		sftpClient1.get("/TEST/list.txt",new File("C:\\Netlang"));
		sftpClient1.close();
 
  • Constructor Details

    • NSFTPClient

      public NSFTPClient(com.netlang.session.NetlangSession session, com.netlang.resourceconfig.ResourceConfig config) throws com.net4z.APIException
      Throws:
      com.net4z.APIException
    • NSFTPClient

      public NSFTPClient(String resourceid) throws com.net4z.APIException
      Throws:
      com.net4z.APIException
    • NSFTPClient

      public NSFTPClient(com.netlang.session.NetlangSession session, String resourceid) throws com.net4z.APIException
      Throws:
      com.net4z.APIException
    • NSFTPClient

      public NSFTPClient(String host, String userid, String password) throws Exception
      Connects to an SSH FTP server with the default port 22.
      Parameters:
      host - the dns name or ip
      userid - the username
      password - the password
      Throws:
      Exception
    • NSFTPClient

      public NSFTPClient(String host, int port, String userid, String password, String knownhosts) throws com.net4z.APIException
      Connects to an SSH FTP server, validating the public key against a list of known hosts. The known host file could be generated with third part programs or manually created using the following format. [,...] ssh-rsa|ssh-dsa
      Parameters:
      host - the dns name or ip
      userid - the username
      password - the password
      port - the port, default 22.
      knownhosts - the path to the knownhosts file.
      Throws:
      Exception
      com.net4z.APIException
    • NSFTPClient

      public NSFTPClient(String host, int port, String userid, String password, String knownhosts, com.netlang.session.NetlangSession nSession, com.netlang.resourceconfig.ResourceConfig config) throws com.net4z.APIException
      Throws:
      com.net4z.APIException
    • NSFTPClient

      public NSFTPClient(String host, int port, String userid, File privateKey) throws Exception
      Throws:
      Exception
    • NSFTPClient

      public NSFTPClient(String host, int port, String userid, String keyfile, String passphrase, String knownhosts) throws Exception
      Creates a connection with public key authentication. Initializes a newly created SFTPConnection
      Parameters:
      host - the dns name or ip
      userid - the username
      port - the port, default 22.
      keyfile - the file to the private and public key in OpenSSH format
      passphrase - the password for the given key
      knownhosts - a list with known hosts or null if no verification should be made.
      Throws:
      Exception
    • NSFTPClient

      public NSFTPClient(String host, int port, String userid, String password) throws Exception
      Connects to an SSH FTP server
      Parameters:
      host - the dns name or ip
      userid - the username
      password - the password
      port - the port, default 22.
      Throws:
      Exception
  • Method Details

    • main

      public static void main(String[] args) throws Exception
      Throws:
      Exception
    • putFolder

      public void putFolder(String from, String to) throws Exception
      Copy all files in the specified folder to the FTP server
      Parameters:
      from - the folder to copy
      to - the folder where to place the files.
      Throws:
      Exception
    • putFile

      public void putFile(String fromfile, String tofile) throws Exception
      Upload a file to the ftp
      Specified by:
      putFile in interface IFTPClient
      Throws:
      Exception
    • get

      public void get(String from, String to, boolean copySubdirs) throws Exception
      Copy an entire folder and optional its subfolders to the local disc.
      Parameters:
      from - the path to copy from
      to - the new path on the local disk
      copySubdirs - if true all subfolders and its content will be copied.
      Throws:
      Exception
    • transferFile

      public void transferFile(String remotefile, String localfile) throws Exception
      Copy the remote file to the local path
      Specified by:
      transferFile in interface IFTPClient
      Parameters:
      remotefile - the file to copy
      localfile - the local file
      Throws:
      Exception
    • listDirs

      public List<NLFile> listDirs(String path) throws Exception
      List all directories in the current directory.
      Parameters:
      pattern - the pattern
      Returns:
      Throws:
      Exception
    • listFiles

      public List<NLFile> listFiles(String path, String pattern) throws Exception
      List all files according to the specified pattern in the current directory
      Parameters:
      pattern - the pattern
      Returns:
      a list with file names
      Throws:
      Exception
    • listFiles

      public List<NLFile> listFiles(String path) throws Exception
      Throws:
      Exception
    • changePermission

      public void changePermission(int permissions, String path) throws Exception
      Parameters:
      path -
      Throws:
      Exception
    • changeDir

      public boolean changeDir(String path) throws Exception
      Description copied from interface: IFTPClient
      Change the current working directory.
      Specified by:
      changeDir in interface IFTPClient
      Parameters:
      path -
      Returns:
      true always otherwise an exception will be thrown.
      Throws:
      Exception
    • revertChangeFolder

      public void revertChangeFolder(int levels) throws Exception
      Traverse back in the folder structure.
      Parameters:
      levels -
      Throws:
      Exception
    • createFolder

      public void createFolder(String path) throws Exception
      Make remote directory
      Specified by:
      createFolder in interface IFTPClient
      Parameters:
      path -
      Throws:
      Exception
    • removeFolder

      public void removeFolder(String path) throws Exception
      delete remote file
      Parameters:
      path -
      Throws:
      Exception
    • removeFile

      public void removeFile(String file) throws Exception
      delete remote file
      Specified by:
      removeFile in interface IFTPClient
      Parameters:
      path -
      Throws:
      Exception
    • close

      public void close()
      Close this connection.
      Specified by:
      close in interface IFTPClient
    • findFile

      public boolean findFile(String path, String pattern) throws Exception
      Check if any files matches the specified pattern.
      Parameters:
      pattern -
      Returns:
      Throws:
      Exception
    • renameFile

      public void renameFile(String origFile, String newFileName) throws Exception
      Rename a file on the FTP
      Parameters:
      origFile -
      newFileName -
      Throws:
      Exception
    • getFileSize

      public long getFileSize(String filename) throws Exception
      Returns the size on disc of the specified file
      Specified by:
      getFileSize in interface IFTPClient
      Parameters:
      filename -
      Returns:
      Throws:
      Exception
    • isConnected

      public boolean isConnected()
      Check if the connection is active.
      Returns:
    • isDirectory

      public boolean isDirectory(String dir)
      Check whether the specified string is a directory.
      Parameters:
      dir -
      Returns:
    • makeRemotedir

      public void makeRemotedir(String basepath, String path) throws Exception
      Create a directory on the FTP
      Parameters:
      basepath -
      path -
      Throws:
      Exception
    • sendCommand

      public String sendCommand(String command, String parameter) throws Exception
      Send a (raw) command to the FTP server.
      Specified by:
      sendCommand in interface IFTPClient
      Parameters:
      command - the command
      parameter - the parameters
      Returns:
      the result from the ftp server
      Throws:
      Exception
    • getFile

      public InputStream getFile(String file) throws Exception
      Description copied from interface: IFTPClient
      Returns a InputStream for the specified file.
      Specified by:
      getFile in interface IFTPClient
      Parameters:
      file - the file at the FTP server
      Returns:
      the content as an inputStream
      Throws:
      Exception
    • list

      public List<NLFile> list(String path, String pattern, int type) throws Exception
      Description copied from interface: IFTPClient
      List all directories that matches the specified pattern.
      Specified by:
      list in interface IFTPClient
      Parameters:
      path - the path
      pattern - the pattern
      Returns:
      Throws:
      Exception
    • transferFile

      public void transferFile(String remotefile, String toLocalFolder, String toLocalFile) throws Exception
      Specified by:
      transferFile in interface IFTPClient
      Throws:
      Exception
    • listFiles

      public List<NLFile> listFiles(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      listFiles in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • listFolders

      public List<String> listFolders(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      listFolders in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • listTree

      public List<NLFile> listTree(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      listTree in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • extract

      public com.netlang.io.filesystem.FileSystemContent extract(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      extract in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • write

      public void write(com.netlang.io.filesystem.FileSystemParm parm, com.netlang.io.filesystem.FileSystemContent content) throws com.net4z.APIException
      Specified by:
      write in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • existFile

      public boolean existFile(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      existFile in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • getFilePath

      public FileReference getFilePath(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      getFilePath in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • mkdir

      public void mkdir(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      mkdir in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • remove

      public void remove(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      remove in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • rename

      public void rename(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      rename in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • lastModifiedFile

      public long lastModifiedFile(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      lastModifiedFile in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • sizeFile

      public long sizeFile(com.netlang.io.filesystem.FileSystemParm parm) throws com.net4z.APIException
      Specified by:
      sizeFile in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • moveOrCopySameSubtype

      public void moveOrCopySameSubtype(com.netlang.io.filesystem.FileSystemParm parm, com.netlang.io.filesystem.FileSystem fromFs) throws com.net4z.APIException
      Specified by:
      moveOrCopySameSubtype in interface com.netlang.io.filesystem.FileSystem
      Throws:
      com.net4z.APIException
    • put

      public void put(NetlangFile from, String path, FileOptions options) throws com.net4z.APIException
      Specified by:
      put in interface IFTPClient
      Throws:
      com.net4z.APIException
    • put

      public void put(File from, String path, FileOptions options) throws com.net4z.APIException
      Specified by:
      put in interface IFTPClient
      Throws:
      com.net4z.APIException
    • put

      public void put(String from, String path, FileOptions options) throws com.net4z.APIException
      Specified by:
      put in interface IFTPClient
      Throws:
      com.net4z.APIException
    • put

      public void put(String resourceid, String frompath, String remotePath, FileOptions options) throws com.net4z.APIException
      Specified by:
      put in interface IFTPClient
      Throws:
      com.net4z.APIException
    • get

      public void get(String path, NetlangFile to) throws com.net4z.APIException
      Specified by:
      get in interface IFTPClient
      Throws:
      com.net4z.APIException
    • get

      public void get(String path, String to) throws com.net4z.APIException
      Specified by:
      get in interface IFTPClient
      Throws:
      com.net4z.APIException
    • get

      public void get(String path, File to) throws com.net4z.APIException
      Specified by:
      get in interface IFTPClient
      Throws:
      com.net4z.APIException
    • get

      public void get(String remotePath, String toResourceId, String toFile) throws com.net4z.APIException
      Specified by:
      get in interface IFTPClient
      Throws:
      com.net4z.APIException