/** * 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); } }
try { SSH_LOG.info("Downloading the file: " + outputFileName); sftp.get(outputFileName, outputFileName); SSH_LOG.info("Download completed");
client.get(remoteFile.getFilename(), out); out.flush(); final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
/************************************************************************/ public static File sftpDownload(FTPConfig config, File file, String remoteFileName) throws IOException { SshClient ssh = new SshClient(); SftpClient sftp = sshLogin(config, ssh); sftp.get(remoteFileName, new FileOutputStream(file)); sftp.quit(); ssh.disconnect(); return file; } /***********************************************************************/
/** * The sftp-client MUST be connected. This method DOES NOT close the * sftp-client * * @param remoteFile * @param client * a sftpclient, MUST be CONNECTED * @return the retrieved file, as a datahandler * @throws IOException * the file is not found or FTP access problem */ private DataHandler getAsDataHandler(SftpFile remoteFile, SftpClient client) throws IOException { DataHandler result; this.logger.info("start receiving file " + remoteFile.getFilename() + " from SFTP server"); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(remoteFile .getAttributes().getSize().intValue()); client.get(remoteFile.getFilename(), outputStream); outputStream.flush(); logger.finest(remoteFile.getFilename() + " file received from SFTP server"); final ByteArrayDataSource datasource = new ByteArrayDataSource(outputStream.toByteArray(), FileTypeMap.getDefaultFileTypeMap().getContentType(remoteFile.getFilename())); outputStream.close(); datasource.setName(remoteFile.getFilename()); result = new DataHandler(datasource); logger.finest(remoteFile.getFilename() + " set as DataHandler"); return result; }