Wednesday, August 12, 2015

Connecting to SFTP


SFTP is a network protocol that provides file operation over network .

It  runs on a secure channel.

Below is a sample code for SFTP actions.



 protected JSch jsch = null ;

    protected Session session = null ;

    protected ChannelSftp sftpChannel = null ;


    /** A convenience method for connecting and logging in */
    public boolean connectAndLogin( String host, String userName, String password, String privateKey, String publicKey ) throws IOException, JSchException
    {
        boolean success = false ;

        try
        {
            System.err.println( "Tryign to SECURELY connect " + host + " with user name " + userName + ", password: ************ " ) ;

            this.jsch = new JSch( ) ;

            // will this work?
            java.security.Security.removeProvider( "SunPKCS11-Solaris" ) ;

            JSch.setLogger( this ) ;

            if( !YClientUtils.isBlankTrim( privateKey ) && !YClientUtils.isBlankTrim( publicKey ) )
            {
                this.jsch.addIdentity( userName, privateKey.getBytes( "utf8" ), publicKey.getBytes( "utf8" ), password == null ? null : password.getBytes( "uft8" ) ) ;
            }
            else
            {
                throw new IllegalArgumentException( "Private and public keys must not be null" ) ;
            }

            this.session = this.jsch.getSession( userName, host ) ;

            this.session.setConfig( "StrictHostKeyChecking", "no" ) ;

            this.session.setConfig( "PreferredAuthentications", "publickey" ) ;
            this.session.setTimeout( 120000 ) ;
           
            this.session.connect( ) ;

            Channel channel = this.session.openChannel( "sftp" ) ;
            channel.connect( ) ;

            this.sftpChannel = ( ChannelSftp )channel ;
            success = true ;
        }
        catch( IOException e )
        {
            e.printStackTrace( ) ;
            success = false ;
            throw e ;
        }
        catch( JSchException e )
        {
            e.printStackTrace( ) ;
            success = false ;
            throw e ;
        }
        finally
        {
            System.err.println( "SECURELY connect status  " + host + " with user name " + userName + " :" + ( success ? "SUCCESS" : "FAIL" ) ) ;
        }

        return success ;
    }

    public void close( )
    {
        if( this.sftpChannel != null )
        {
            this.sftpChannel.exit( ) ;
        }

        if( this.session != null )
        {
            this.session.disconnect( ) ;
        }
    }

    public boolean downloadFile( String remoteFile, String localFile )
    {
        try
        {
            this.sftpChannel.get( remoteFile, localFile ) ;

            return true ;
        }
        catch( Exception e )
        {
            e.printStackTrace( ) ;

            return false ;
        }
    }

    public boolean uploadFile( String localFile, String remoteFile )
    {
        try
        {
            this.sftpChannel.put( localFile, remoteFile ) ;

            return true ;
        }
        catch( Exception e )
        {
            e.printStackTrace( ) ;

            return false ;
        }
    }

    public boolean deleteFile( String remoteFile, int tryCount ) throws IOException
    {
        int count = 0 ;

        while( count++ < tryCount )
        {
            try
            {
                this.sftpChannel.rm( remoteFile ) ;

                return true ;
            }
            catch( Exception e )
            {
                System.err.println( "Trial " + count ) ;
                e.printStackTrace( ) ;
            }
        }

        return false ;
    }

No comments:

Post a Comment