Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.rsmg.mi.ftp;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.commons.vfs2.FileObject;
- import org.apache.commons.vfs2.FileSystemOptions;
- import org.apache.commons.vfs2.Selectors;
- import org.apache.commons.vfs2.impl.StandardFileSystemManager;
- import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Service;
- @Service
- @Qualifier("sftpService")
- public class SftpService implements IFtpService {
- private static Logger logger = LoggerFactory.getLogger(SftpService.class);
- @Override
- public List<String> getBackupInfo(String server, String login, String password) throws FtpServiceException {
- List<String> result = new ArrayList<String>();
- return result;
- }
- @Override
- public boolean deleteFileFromFtp(String server, int port, String login, String password, String remoteFile, boolean ssl) throws FtpServiceException {
- boolean success = false;
- return success;
- }
- @Override
- public boolean uploadFileToFtp(String server, int port, String login, String password, String remoteFile, String localFile, boolean ssl) throws FtpServiceException {
- StandardFileSystemManager manager = new StandardFileSystemManager();
- try {
- // 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);
- // check if the file exists
- //String filepath = localDirectory + fileToFTP;
- File file = new File(localFile);
- //if (!file.exists()) throw new RuntimeException("Error. Local file not found");
- // Create the SFTP URI using the host name, userid, password, remote path and
- // file name
- String sftpUri = "sftp://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteFile;
- // Create local file object
- FileObject localFileObj = manager.resolveFile(file.getAbsolutePath());
- // Create remote file object
- FileObject remoteFileObj = manager.resolveFile(sftpUri, opts);
- // Copy local file to sftp server
- remoteFileObj.copyFrom(localFileObj, Selectors.SELECT_SELF);
- //System.out.println("File upload successful");
- } catch (Exception ex) {
- ex.printStackTrace();
- return false;
- } finally {
- manager.close();
- }
- return true;
- }
- @Override
- public boolean downloadFileFromFtp(String server, int port, String login, String password, String remoteFile, String localFile, boolean ssl, boolean deleteFile) throws FtpServiceException {
- StandardFileSystemManager manager = new StandardFileSystemManager();
- try {
- // 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);
- // check if the file exists
- //String filepath = localDirectory + fileToFTP;
- File file = new File(localFile);
- //if (!file.exists()) throw new RuntimeException("Error. Local file not found");
- // Create the SFTP URI using the host name, userid, password, remote path and
- // file name
- String sftpUri = "sftp://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteFile;
- // Create local file object
- FileObject localFileObj = manager.resolveFile(file.getAbsolutePath());
- // Create remote file object
- FileObject remoteFileObj = manager.resolveFile(sftpUri, opts);
- // Copy local file to sftp server
- localFileObj.copyFrom(remoteFileObj, Selectors.SELECT_SELF);
- //System.out.println("File upload successful");
- } catch (Exception ex) {
- ex.printStackTrace();
- return false;
- } finally {
- manager.close();
- }
- return true;
- }
- @Override
- public boolean checkIfDirectoryExists(String server, int port, String login, String password, String remoteDirectoryPath, boolean ssl) throws FtpServiceException {
- StandardFileSystemManager manager = new StandardFileSystemManager();
- boolean success = false;
- try {
- // 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://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteDirectoryPath;
- // List all the files in that directory.Try to give the directory path
- FileObject remoteDirectory=manager.resolveFile(sftpUri);
- //Check if the file exists
- if(remoteDirectory.exists()){
- success = true;
- } else {
- success = false;
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- manager.close();
- }
- return success;
- }
- @Override
- public Map<String, Long> listDirectoryFiles(String server, int port, String login, String password, String remoteDirectoryPath, boolean ssl) throws FtpServiceException {
- StandardFileSystemManager manager = new StandardFileSystemManager();
- Map<String, Long> resultFiles = new LinkedHashMap<>();
- try {
- // 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://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteDirectoryPath;
- // List all the files in that directory.Try to give the directory path
- FileObject localFileObject=manager.resolveFile(sftpUri);
- FileObject[] children = localFileObject.getChildren();
- for ( int i = 0; i < children.length; i++ ){
- //System.out.println( children[ i ].getName().getBaseName() + children[i].getContent().getSize() );
- resultFiles.put(children[ i ].getName().getBaseName(), children[i].getContent().getSize() );
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- manager.close();
- }
- return resultFiles;
- }
- @Override
- public boolean createNewRemoteDirectory(String server, int port, String login, String password, String remoteDirectoryPath, boolean ssl) throws FtpServiceException {
- StandardFileSystemManager manager = new StandardFileSystemManager();
- boolean success = false;
- try {
- // 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://" + login + ":" + password + "@" + server + ":"+ port +"/" + remoteDirectoryPath;
- // List all the files in that directory.Try to give the directory path
- FileObject remoteDirectory=manager.resolveFile(sftpUri);
- remoteDirectory.createFolder();
- success = true;
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- manager.close();
- }
- return success;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement