简体   繁体   English

解释在使用JSch的SFTP文件传输中使用的路径

[英]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 LOCALDIR
  • targetDir TARGETDIR
  • localFileName localFileName
  • localAbsoluteFileName localAbsoluteFileName

I want to transfer a file test.csv from server to client, opening SFTP channel from client. 我想将文件test.csv从服务器传输到客户端,从客户端打开SFTP通道。

File is at the following location in server "10.10.20.30" 文件位于服务器“ 10.10.20.30”中的以下位置

/ram/server/files/test.csv

which needs to copied to the following location in client "10.10.20.40" 需要复制到客户端“ 10.10.20.40”中的以下位置

/ram/client/files

what is localDir , targetDir , localFileName , localAbsoluteFileName here and what does the following steps does 什么是localDirtargetDirlocalFileNamelocalAbsoluteFileName以及以下步骤的作用

sftpChannel.cd( targetDir );
sftpChannel.lcd( localDir );
  • localDir - The local working directory. localDir本地工作目录。 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 . 由于相对路径是“此路径”(点),因此可以有效地将文件下载到localDir You should set this to /ram/client/files . 您应该将其设置为/ram/client/files
  • targetDir - The remote working directory. targetDir远程工作目录。 If the localAbsoluteFileName is really absolute, the remote working directory is irrelevant. 如果localAbsoluteFileName确实是绝对的,则远程工作目录无关。 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). 删除使用( .cd调用),或者,如果您不想更改实现,请将其设置为/ram/server/files (或您可以访问的任何其他路径)。 Despite the name ("target"), it would actually be a source directory, if it were used. 尽管使用了名称(“目标”),但如果使用了它,它实际上将是源目录。
  • localFileName - Used for logging only. localFileName仅用于记录。
  • localAbsoluteFileName - Despite the "local" in the name, it used as a source path to the remote file to download. localAbsoluteFileName尽管名称中为“ local”,但它仍用作要下载的远程文件的源路径。 You should set this to /ram/server/files/test.csv . 您应该将此设置为/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. 似乎有人在上载代码,将其更改为下载代码(用.get替换.put ),同时保留用于上载的参数命名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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