@ Nonnull public static ESuccess execute ( @ Nonnull final IJSchSessionProvider aSessionProvider , final int nChannelConnectTimeoutMillis , @ Nonnull final IChannelSftpRunnable aRunnable ) throws JSchException { ValueEnforcer . notNull ( aSessionProvider , "SessionProvider" ) ; ValueEnforcer . notNull ( aRunnable , "Runnable" ) ; Session aSession = null ; Channel aChannel = null ; ChannelSftp aSFTPChannel = null ; try { // get session from pool aSession = aSessionProvider . createSession ( ) ; if ( aSession == null ) throw new IllegalStateException ( "Failed to create JSch session from provider" ) ; // Open the SFTP channel aChannel = aSession . openChannel ( "sftp" ) ; // Set connection timeout aChannel . connect ( nChannelConnectTimeoutMillis ) ; aSFTPChannel = ( ChannelSftp ) aChannel ; // call callback aRunnable . execute ( aSFTPChannel ) ; return ESuccess . SUCCESS ; } catch ( final SftpException ex ) { LOGGER . error ( "Error peforming SFTP action: " + aRunnable . getDisplayName ( ) , ex ) ; return ESuccess . FAILURE ; } finally { // end SFTP session if ( aSFTPChannel != null ) aSFTPChannel . quit ( ) ; // close channel if ( aChannel != null && aChannel . isConnected ( ) ) aChannel . disconnect ( ) ; // destroy session if ( aSession != null ) JSchSessionFactory . destroySession ( aSession ) ; } }
Upload a file to the server .