Tuesday, October 28, 2014

SFTP in JAVA

Following JAVA functions can be used to execute SFTP(Secure FTP).

    public boolean putSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) throws FileSystemException {

        boolean put = false;
        StandardFileSystemManager manager = new StandardFileSystemManager();

        try {

            String filepath = localDirectory + fileName;
            File file = new File(filepath);
            if (!file.exists()) {
                throw new RuntimeException("Error. Local file not found.");
            }
            manager.init();

            FileSystemOptions opts = new FileSystemOptions();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
                    opts, "no");
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

            String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory + fileName;

            FileObject localFile = manager.resolveFile(file.getAbsolutePath());
            System.out.println(file.getAbsolutePath());

            FileObject remoteFile = manager.resolveFile(sftpUri, opts);

            remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
            put = true;

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            manager.close();
        }
        return put;
    }

    public boolean getSFTPFile(String sftpHost, String userID, String password, String remoteDirectory, String localDirectory, String fileName) {

        boolean get = false;
        StandardFileSystemManager manager = new StandardFileSystemManager();

        try {

            manager.init();

            FileSystemOptions opts = new FileSystemOptions();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
                    opts, "no");
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

            String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
                    + remoteDirectory + fileName;

            String filepath = localDirectory + fileName;
            File file = new File(filepath);
            FileObject localFile = manager.resolveFile(file.getAbsolutePath());

            FileObject remoteFile = manager.resolveFile(sftpUri, opts);

            localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
            get = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            manager.close();
        }
        return get;
    }

    public boolean deleteSFTFile(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {

        boolean delete = false;
        StandardFileSystemManager manager = new StandardFileSystemManager();

        try {

            manager.init();

            FileSystemOptions opts = new FileSystemOptions();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
                    opts, "no");
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

            String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
                    + remoteDirectory + fileName;

            FileObject remoteFile = manager.resolveFile(sftpUri, opts);

            if (remoteFile.exists()) {
                if (remoteFile.delete()) {
                    delete = true;
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            manager.close();
        }
        return delete;
    }

    public Collection getSFTPFileList(String sftpHost, String userID, String password, String remoteDirectory) throws FileSystemException {

        Collection fileCollection = new ArrayList();
        StandardFileSystemManager manager = new StandardFileSystemManager();

        FileObject localFileObject = manager.resolveFile("sftp://" + userID + ":" + password + "@" + sftpHost + "/" + remoteDirectory);
        FileObject[] children = localFileObject.getChildren();

        for (int i = 0; i < children.length; i++) {
            fileCollection.add(children[ i].getName().getBaseName());
        }

        return fileCollection;
    }

    public boolean isRemoteFileExist(String sftpHost, String userID, String password, String remoteDirectory, String fileName) {
        StandardFileSystemManager manager = new StandardFileSystemManager();
        boolean isExist = false;

        try {
            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);

            String sftpUri = "sftp://" + userID + ":" + password + "@" + sftpHost + "/"
                    + remoteDirectory + fileName;

            FileObject remoteFile = manager.resolveFile(sftpUri, opts);

            isExist = remoteFile.exists();

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            manager.close();
        }
        return isExist;
    }

No comments:

Post a Comment