gpt4 book ai didi

java - sshtools.SftpClient.put 失败,错误代码 "No such file"

转载 作者:行者123 更新时间:2023-12-01 14:32:00 26 4
gpt4 key购买 nike

我继承了一个基于 Java 的项目,其中包含一个 cron 作业,用于通过 SFTP 将文件上传到第三方服务器。这是相关代码。

String filePath = IUtil.getInstance().getProperties("cheetah_sftp_filepath");
try{
SshClient ssh = new SshClient();
ssh.connect(host, port);
//Authenticate
PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
passwordAuthenticationClient.setUsername(userName);
passwordAuthenticationClient.setPassword(password);
int result = ssh.authenticate(passwordAuthenticationClient);
if(result != AuthenticationProtocolState.COMPLETE){
throw new Exception("Login to " + host + ":" + port + " " + userName + "/" + password + " failed");
}
//Open the SFTP channel
SftpClient client = ssh.openSftpClient();
client.cd("autoproc");
client.put(filePath);
//disconnect
client.quit();
ssh.disconnect();
} catch(Exception e) {
String message = "Failed during sftp: " + e.getMessage();
addJobMessage(message, JobMessageType.JOB_MESSAGE_TYPE_ERROR);
e.printStackTrace();
return false;
}

非常简单,但它不起作用。当 client.put() 执行时,它失败并显示“java.io.IOException: No such file”。这是堆栈跟踪。

java.io.IOException: No such file
at com.sshtools.j2ssh.sftp.SftpSubsystemClient.getOKRequestStatus(Unknown Source)
at com.sshtools.j2ssh.sftp.SftpSubsystemClient.setAttributes(Unknown Source)
at com.sshtools.j2ssh.sftp.SftpSubsystemClient.changePermissions(Unknown Source)
at com.sshtools.j2ssh.SftpClient.chmod(Unknown Source)
at com.sshtools.j2ssh.SftpClient.put(Unknown Source)
at com.sshtools.j2ssh.SftpClient.put(Unknown Source)
at com.sshtools.j2ssh.SftpClient.put(Unknown Source)
at com.sshtools.j2ssh.SftpClient.put(Unknown Source)
at com.dez.store.scripts.SendEmailShellCommand.sftpToCheetah(SendEmailToCheetahShellCommand.java:99)
at com.dez.store.scripts.SendEmailShellCommand.execute(SendEmailToCheetahShellCommand.java:34)
at com.fry.ocp.common.ShellCommandExecutor.main(ShellCommandExecutor.java:90)

filePath 是文件的绝对路径。是的,我已经检查了显而易见的事情:路径正确并且文件存在。文件权限为 664,因此读取在任何情况下都不会失败,但该进程以 root 身份运行来启动。

我已经尝试了我能想到的一切。

  • 我已经检查了目录树(有点深)的读取权限。
  • 我已尝试使用 lcd() 访问该目录,一旦到达该目录,就会使用 lpwd()。这看起来不错,但 put 仍然失败,因此长路径名似乎不是问题。
  • 我仔细检查以确保最初写入该文件的文件流都已正确关闭。我没有看到任何让我认为这可能是问题的东西。
  • 我尝试使用完整路径创建 j2ssh.sftp.SftpFile 实例,以查看它是否可以访问该文件,并确保“没有此类文件”错误与远程无关主持人。当我执行 SftpFile.canRead() 时,出现空指针异常,因此我认为这是本地问题。

我已经很多年没有接触过 Java 了。说我生锈是轻描淡写的。然而,我们最后一个“Java 人”刚刚退出,我是店里唯一接触过 Java 的人,所以我是新的“Java 人”。

我错过了一些简单的事情吗?还有其他想法吗?

-肖恩

最佳答案

根据调用堆栈和您的描述,我预计错误是指远程文件。

只有在传输完成后才必须执行chmod,因此我假设SftpClient相信传输已完成并尝试更新远程文件权限。看起来它失败了,因为该文件实际上不存在。收到错误后,请使用 SftpClient.ls() 检查该文件是否存在。很可能您有一些远程端进程会在上传完成后立即删除文件。

作为解决方法,您还可以尝试阻止 SftpClient 在上传完成后尝试修改权限。我不知道J2SSH。快速浏览了一下,我还没有找到任何用于此目的的 API。也许是 SftpClient.umask()

您可以尝试切换到JSch 。上传后似乎没有隐式 chmod。

此外,还值得检查远程服务器日志。

关于java - sshtools.SftpClient.put 失败,错误代码 "No such file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16803245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com