/** * 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); } }
/** * 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); }
String localdir = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator"); SftpClient sftp = getSftpClient(host, port,username, password); sftp.cd(fileLocation); sftp.lcd(localdir); @SuppressWarnings("rawtypes")
/** * Create directory on remote server. * * @param operationName * The remote directory creation is not available for all operation. * @throws IOException * if any IO problem occur while sending file or connecting to server */ public void createRemoteDirectoryIfNeeded(final String operationName) throws IOException { final String directory = this.info.getDirectory(); if (this.info.isCreateFolder() && !StringHelper.isNullOrEmpty(directory) && ((SFTPConstants.MPUT.equalsIgnoreCase(operationName)) || SFTPConstants.PUT.equalsIgnoreCase(operationName))) { // write operation : create all necessary folders try { this.client.cd(directory); } catch (final FileNotFoundException e) { // In principle, this exception occurs when the remote directory does not exist. this.client.mkdirs(directory); } } }