/** * Gets the file from remote host. * * @param host the hostname of the client * @param port the port number the user want to use for connection * @param username the username required for authentication * @param password the password required for authentication * @param remoteFilePath remotePath the remote path from where the file is going to be get * @param localfilePath the local file path where the file is going to be put * @param fileName the name of the file you want to transfer * @return the file from the remote host * @throws IOException Signals that an I/O exception has occurred. */ public static void getFileFromRemoteHost(String host, int port,String username,String password, String remoteFilePath,String localPath,String fileName) throws IOException { SftpClient sftp = getSftpClient(host, port,username, password); sftp.cd(remoteFilePath); sftp.lcd(localPath); sftp.get(fileName); File f = new File(localPath+System.getProperty("file.separator")+fileName); if (!f.exists()){ SSH_LOG.error("The file " + fileName + " was not tranferred to " + localPath); } else{ SSH_LOG.info("The file " + fileName + " was tranferred successfully to " + localPath); } }
SftpClient sftp = getSftpClient(host, port,username, password); sftp.cd(fileLocation); sftp.lcd(localdir); @SuppressWarnings("rawtypes") List listinbox = sftp.ls();
/** * Put file to remote host. * * @param host the hostname of the client * @param port the port number the user want to use for connection * @param username the username required for authentication * @param password the password required for authentication * @param localfilePath the localfile path where the file is situated * @param remotePath the remote path where the file is going to be put * @param fileName the file name you want to put * @throws IOException Signals that an I/O exception has occurred. */ @SuppressWarnings("unchecked") public static void putFileToRemoteHost(String host, int port,String username,String password, String localfilePath,String remotePath,String fileName)throws IOException { SftpClient sftp = getSftpClient(host,port, username, password); sftp.lcd(localfilePath); sftp.cd(remotePath); sftp.put(fileName); List<SftpFile> dirContents = sftp.ls(remotePath); Iterator<SftpFile> it = dirContents.iterator(); while(it.hasNext()){ if(it.next().getFilename().equals(fileName)){ SSH_LOG.info("The file " + fileName + " was tranferred successfully to " + remotePath); return; } } SSH_LOG.error("The file " + fileName + " was not tranferred to " + remotePath); }