List of usage examples for org.apache.commons.vfs2.impl StandardFileSystemManager StandardFileSystemManager
StandardFileSystemManager
From source file:sftpexamples.GetMyFiles.java
public boolean startFTP(String propertiesFilename, String fileToDownload) { props = new Properties(); StandardFileSystemManager manager = new StandardFileSystemManager(); try {/*from ww w .jav a 2 s .co m*/ props.load(new FileInputStream(System.getProperty("user.home") + propertiesFilename)); String serverAddress = props.getProperty("serverAddress").trim(); String userId = props.getProperty("userId").trim(); String password = props.getProperty("password").trim(); String remoteDirectory = props.getProperty("remoteDirectory").trim(); String localDirectory = props.getProperty("localDirectory").trim(); //Initializes the file manager manager.init(); //Setup our SFTP configuration FileSystemOptions opts = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true); SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); //Create the SFTP URI using the host name, userid, password, remote path and file name //String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress // + "/" + remoteDirectory + fileToDownload; String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + fileToDownload; // Create local file object String filepath = localDirectory + fileToDownload; File file = new File(filepath); FileObject localFile = manager.resolveFile(file.getAbsolutePath()); // Create remote file object FileObject remoteFile = manager.resolveFile(sftpUri, opts); // Copy local file to sftp server localFile.copyFrom(remoteFile, Selectors.SELECT_SELF); System.out.println("File download successful"); } catch (Exception ex) { ex.printStackTrace(); return false; } finally { manager.close(); } return true; }
From source file:sftpexamples.SendMyFiles.java
public boolean startFTP(String propertiesFilename, String fileToFTP) { props = new Properties(); StandardFileSystemManager manager = new StandardFileSystemManager(); try {// w ww.jav a 2 s . c om props.load(new FileInputStream(System.getProperty("user.home") + propertiesFilename)); //props.setProperty("serverAddress", "127.0.0.1"); String serverAddress = props.getProperty("serverAddress").trim(); String userId = props.getProperty("userId").trim(); String password = props.getProperty("password").trim(); String remoteDirectory = props.getProperty("remoteDirectory").trim(); String localDirectory = props.getProperty("localDirectory").trim(); System.out.println("properties are fetched:serverAddress=" + serverAddress); System.out.println("userId=" + userId); System.out.println("password=" + password); System.out.println("remoteDirectory=" + remoteDirectory); System.out.println("localDirectory=" + localDirectory); //check if the file exists String filepath = localDirectory + fileToFTP; System.out.println("filepath:" + filepath); File file = new File(filepath); if (!file.exists()) { throw new RuntimeException("Error. Local file not found"); } //Initializes the file manager manager.init(); //Setup our SFTP configuration FileSystemOptions opts = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true); SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); //Create the SFTP URI using the host name, userid, password, remote path and file name String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + fileToFTP; System.out.println("uri: " + sftpUri); // Create local file object FileObject localFile = manager.resolveFile(file.getAbsolutePath()); // Create remote file object FileObject remoteFile = manager.resolveFile(sftpUri, opts); // Copy local file to sftp server remoteFile.copyFrom(localFile, Selectors.SELECT_SELF); System.out.println("File upload successful"); } catch (Exception ex) { ex.printStackTrace(); return false; } finally { manager.close(); } return true; }
From source file:sftpexamples.SftpUtility.java
/** * Method to upload a file in Remote server * * @param hostName HostName of the server * @param username UserName to login/*from w ww . j a v a2 s. c om*/ * @param password Password to login * @param localFilePath LocalFilePath. Should contain the entire local file * path - Directory and Filename with \\ as separator * @param remoteFilePath remoteFilePath. Should contain the entire remote * file path - Directory and Filename with / as separator */ public static void upload(String hostName, String username, String password, String localFilePath, String remoteFilePath) { File file = new File(localFilePath); if (!file.exists()) { throw new RuntimeException("Error. Local file not found"); } StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Create local file object FileObject localFile = manager.resolveFile(file.getAbsolutePath()); // Create remote file object FileObject remoteFile = manager.resolveFile( createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions()); /* * use createDefaultOptions() in place of fsOptions for all default * options - Ashok. */ // Copy local file to sftp server remoteFile.copyFrom(localFile, Selectors.SELECT_SELF); System.out.println("File upload success"); } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } }
From source file:sftpexamples.SftpUtility.java
public static boolean move(String hostName, String username, String password, String remoteSrcFilePath, String remoteDestFilePath) { StandardFileSystemManager manager = new StandardFileSystemManager(); try {// w w w . j av a 2s.c om manager.init(); // Create remote object FileObject remoteFile = manager.resolveFile( createConnectionString(hostName, username, password, remoteSrcFilePath), createDefaultOptions()); FileObject remoteDestFile = manager.resolveFile( createConnectionString(hostName, username, password, remoteDestFilePath), createDefaultOptions()); if (remoteFile.exists()) { remoteFile.moveTo(remoteDestFile); ; System.out.println("Move remote file success"); return true; } else { System.out.println("Source file doesn't exist"); return false; } } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } }
From source file:sftpexamples.SftpUtility.java
/** * Method to download the file from remote server location * * @param hostName HostName of the server * @param username UserName to login// w w w . jav a 2 s . c o m * @param password Password to login * @param localFilePath LocalFilePath. Should contain the entire local file * path - Directory and Filename with \\ as separator * @param remoteFilePath remoteFilePath. Should contain the entire remote * file path - Directory and Filename with / as separator */ public static void download(String hostName, String username, String password, String localFilePath, String remoteFilePath) { StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Append _downlaod_from_sftp to the given file name. //String downloadFilePath = localFilePath.substring(0, localFilePath.lastIndexOf(".")) + "_downlaod_from_sftp" + localFilePath.substring(localFilePath.lastIndexOf("."), localFilePath.length()); // Create local file object. Change location if necessary for new downloadFilePath FileObject localFile = manager.resolveFile(localFilePath); // Create remote file object FileObject remoteFile = manager.resolveFile( createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions()); // Copy local file to sftp server localFile.copyFrom(remoteFile, Selectors.SELECT_SELF); System.out.println("File download success"); } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } }
From source file:sftpexamples.SftpUtility.java
/** * Method to delete the specified file from the remote system * * @param hostName HostName of the server * @param username UserName to login/*from www . j ava 2 s . c o m*/ * @param password Password to login * @param localFilePath LocalFilePath. Should contain the entire local file * path - Directory and Filename with \\ as separator * @param remoteFilePath remoteFilePath. Should contain the entire remote * file path - Directory and Filename with / as separator */ public static void delete(String hostName, String username, String password, String remoteFilePath) { StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Create remote object FileObject remoteFile = manager.resolveFile( createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions()); if (remoteFile.exists()) { remoteFile.delete(); System.out.println("Delete remote file success"); } } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } }
From source file:sftpexamples.SftpUtility.java
/** * Method to check if the remote file exists in the specified remote * location/*w w w.ja v a 2 s . com*/ * * @param hostName HostName of the server * @param username UserName to login * @param password Password to login * @param remoteFilePath remoteFilePath. Should contain the entire remote * file path - Directory and Filename with / as separator * @return Returns if the file exists in the specified remote location */ public static boolean exist(String hostName, String username, String password, String remoteFilePath) { StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); // Create remote object FileObject remoteFile = manager.resolveFile( createConnectionString(hostName, username, password, remoteFilePath), createDefaultOptions()); System.out.println("File exist: " + remoteFile.exists()); return remoteFile.exists(); } catch (Exception e) { throw new RuntimeException(e); } finally { manager.close(); } }
From source file:si.matjazcerkvenik.dtools.tools.ftp.VfsFtpSftpClient.java
public void upload(String localFile, String remoteFile) { StandardFileSystemManager manager = new StandardFileSystemManager(); try {/*from w ww. j a v a 2s . c om*/ // check if the file exists File file = new File(localFile); if (!file.exists()) throw new RuntimeException("Error. Local file not found"); // Initializes the file manager manager.init(); // Setup our SFTP configuration FileSystemOptions opts = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true); SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); System.out.println("copy " + file.getAbsolutePath() + " to"); // Create the SFTP URI using the host name, userid, password, remote // path and file name String sftpUri = protocol + "://" + username + ":" + password + "@" + hostname + ":" + port + remoteFile; System.out.println(sftpUri); // Create local file object FileObject localFileObject = manager.resolveFile(file.getAbsolutePath()); // Create remote file object FileObject remoteFileObject = manager.resolveFile(sftpUri, opts); // Copy local file to sftp server remoteFileObject.copyFrom(localFileObject, Selectors.SELECT_SELF); System.out.println("File upload successful"); } catch (Exception ex) { ex.printStackTrace(); } finally { manager.close(); } }
From source file:si.matjazcerkvenik.dtools.tools.ftp.VfsFtpSftpClient.java
public void download(String localFile, String remoteFile) { StandardFileSystemManager manager = new StandardFileSystemManager(); // Initializes the file manager try {/* w w w .j ava2 s . c o m*/ manager.init(); // Setup our SFTP configuration FileSystemOptions opts = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no"); SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true); SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000); // Create the SFTP URI using the host name, userid, password, remote // path and file name String sftpUri = protocol + "://" + username + ":" + password + "@" + hostname + ":" + port + remoteFile; System.out.println(sftpUri); // Create local file object File file = new File(localFile); FileObject localFileObject = manager.resolveFile(file.getAbsolutePath()); // Create remote file object FileObject remoteFileObject = manager.resolveFile(sftpUri, opts); // Copy local file to sftp server localFileObject.copyFrom(remoteFileObject, Selectors.SELECT_SELF); System.out.println("File download successful"); } catch (FileSystemException e) { e.printStackTrace(); } finally { manager.close(); } }
From source file:tw.edu.sinica.iis.SSHadoop.SSHSftp.java
public SSHSftp(final String user, final String host) { manager = new StandardFileSystemManager(); setSSHUser(user); setSSHHost(host); }