简体   繁体   中英

Explaining path used in SFTP file transfer using JSch

public static void sftpFile( String localDir, 
        String localFileName,
        String localAbsoluteFileName,
        String targetHost, 
        int targetPort,
        String targetDir,
        String identityFile,
        String targetUserId, 
        String targetPassword) throws ErrorCodeException
{

    Session session = null;
    ChannelSftp sftpChannel = null;
    JSch jsch = new JSch();        
    try
    {
        session = jsch.getSession( targetUserId, targetHost, targetPort );
        session.setConfig( "StrictHostKeyChecking", "no" );

        if( identityFile != null )
        {
            jsch.addIdentity( identityFile );
        }

        if( targetPassword != null )
        {
            session.setPassword( targetPassword );
        }

        session.connect();

        Channel channel = session.openChannel( "sftp" );
        channel.connect();
        sftpChannel = (ChannelSftp) channel;
        sftpChannel.cd( targetDir );
        sftpChannel.lcd( localDir );

        sftpChannel.get( localAbsoluteFileName, "." );
        if( log.isTraceEnabled() )
        {
            log.trace( tracePrefix + localFileName + ": Done SFTP" );
        }
    }
    catch( Exception e )
    {
        System.out.println( "Error connecting to target host." );
        throw new ErrorCodeException(ErrorCode.COMMUNICATION_FAILURE);
    }
    finally
    {
        if ( sftpChannel != null )
        {
            sftpChannel.exit();
        }
        if ( session != null )
        {
            session.disconnect();
        }
    }

}

I do not have a clear view of the following terms

  • localDir
  • targetDir
  • localFileName
  • localAbsoluteFileName

I want to transfer a file test.csv from server to client, opening SFTP channel from client.

File is at the following location in server "10.10.20.30"

/ram/server/files/test.csv

which needs to copied to the following location in client "10.10.20.40"

/ram/client/files

what is localDir , targetDir , localFileName , localAbsoluteFileName here and what does the following steps does

sftpChannel.cd( targetDir );
sftpChannel.lcd( localDir );
  • localDir - The local working directory. The code uses a relative destination path (the . ), which is resolved relatively to the local working directory. As the relative path is "this path" (the dot), you effectively download the file to the localDir . You should set this to /ram/client/files .
  • targetDir - The remote working directory. If the localAbsoluteFileName is really absolute, the remote working directory is irrelevant. Either remove the use (the .cd call) or, if you do not want to change the implementation, set it to the /ram/server/files (or any other path you have an access to). Despite the name ("target"), it would actually be a source directory, if it were used.
  • localFileName - Used for logging only.
  • localAbsoluteFileName - Despite the "local" in the name, it used as a source path to the remote file to download. You should set this to /ram/server/files/test.csv .

Generally the naming of the parameters seems wrong. It looks like someone took uploading code, changed it to downloading code (replacing a .put with the .get ), while keeping the parameter naming used for the upload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM