11

I'm trying to use Apache VFS2 to upload files to SFTP server. SFTP works OK when using client like WinSCP. I took some examples found in the internet to use the java client, but I'm keep getting errors. The version used is 2.3. The code:

public class SftpPersister
{
    private static final Logger logger          = Logger.getLogger( SftpPersister.class );

    String                      serverAddress   = "ftp.domain.com";
    String                      user            = "myuser";
    String                      password        = "mypass";
    String                      remoteDirectory = "outgoing/";
    String                      localDirectory  = "c:/users/user/";

   public static void main( String[] args )
   {
      new SftpPersister().upload( "ntuser.ini" );
   }

   public boolean upload( String fileName )
   {
      StandardFileSystemManager manager = new StandardFileSystemManager();

      try
      {

        //check if the file exists
        String filepath = localDirectory + fileName;
        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:///" + user + ":" + password + "@" + serverAddress + "/" + remoteDirectory + fileName;

        // 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 );

        logger.info( "File upload successful" );

    }
    catch ( Exception e )
    {
        logger.error( "failure", e );

        return false;
    }
    finally
    {
        manager.close();
    }

    return true;
}

}

Exception is thrown when executing the line remoteFile.copyFrom( localFile, Selectors.SELECT_SELF ) :

1 [main] ERROR com.company.middletier.storage.SftpPersister  - failure
org.apache.commons.vfs2.FileSystemException: Could not find file with URI "sftp:///myuser:***@ftp.domain.com/outgoing/ntuser.ini" because it is a relative path, and no base URI was provided.
at org.apache.commons.vfs2.FileSystemException.requireNonNull(FileSystemException.java:87)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:733)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:653)
at com.company.middletier.storage.SftpPersister.upload(SftpPersister.java:63)
at com.company.middletier.storage.SftpPersister.main(SftpPersister.java:31)

Is it some configuration issue or something else I'm missing?

Seffy
  • 1,045
  • 1
  • 13
  • 27

3 Answers3

17

It seems like jsch is a required runtime dependency that needed to be added to the POM file. Not sure is mentioned in the docs. Once added all works smoothly.

Seffy
  • 1,045
  • 1
  • 13
  • 27
6
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>${jsch.version}</version>
    </dependency>

Implicity dependency to jsch helped me with that problem

lolcio
  • 346
  • 5
  • 12
0

For Gradle builds add below dependency to build.gradle file. compile group: 'com.jcraft', name: 'jsch', version: '0.1.55'

unfortunately jcraft dependency with implemention as mentioned below didn't work for me implementation group: 'com.jcraft', name: 'jsch', version: '0.1.44-1'