[英]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 我对以下术语没有清晰的认识
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 什么是
localDir
, targetDir
, localFileName
, localAbsoluteFileName
以及以下步骤的作用
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.