sshtools.SftpClient.put失败,并显示“无此文件". [英] sshtools.SftpClient.put failing with "No such file"

查看:315
本文介绍了sshtools.SftpClient.put失败,并显示“无此文件".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've inherited a Java based project that includes a cron job to upload a file via SFTP to a third-party server. Here's the relevant code.

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:无此类文件".这是堆栈跟踪.

Pretty straightforward, but it's not working. When client.put() executes, it fails with "java.io.IOException: No such file". Here's the stack trace.

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用户身份启动以启动.

filePath is an absolute path to the file. Yes, I've checked the obvious: the path is correct and the file exists. File permissions are 664 so a read shouldn't be failing in any case, but the process is running as root to boot.

我已经尽力想到了一切.

I've tried everything I can think of.

  • 我已经检查了目录树上的读取权限(很深).
  • 我已经尝试了lcd()到目录,一旦到达目录lpwd().看起来不错,但是put仍然失败,因此长路径名称似乎不是问题.
  • 我仔细检查了一下,以确保最初写入文件的文件流都已正确关闭.我没有看到任何让我认为这可能是问题的东西.
  • 我尝试使用完整路径创建和创建j2ssh.sftp.SftpFile实例,以查看它是否可以访问文件,并确保"No such file"错误与远程主机无关.当我执行SftpFile.canRead()时,我得到一个空指针异常,因此我认为这是一个本地问题.
  • I've checked read permissions all the way up the directory tree (which is kind of deep).
  • I've tried lcd() to the directory and once I get there lpwd(). That seems fine, but put still fails so a long path name doesn't seem to be the issue.
  • I double checked to make sure the file streams that originally wrote the file were all being closed correctly. I don't see anything that makes me think that could be the issue.
  • I tried creating and instance of j2ssh.sftp.SftpFile with the full path to see if it could access the file and make sure the "No such file" error wasn't related to the remote host. When I execute SftpFile.canRead() I get a null pointer exception, so I'm thinking it's a local problem.

多年来我都没有接触过Java.如果说我生锈,那就太轻描淡写了.但是,我们最后一个"Java专家"刚刚辞职,我是店里唯一剩下的曾经曾经接触过Java的人,所以我是新的"Java专家".

I haven't touched Java in years. To say that I am rusty would be a gross understatement. However, our last "Java guy" just quit and I'm the only person left in my shop who has touched Java ever, so I'm the new "Java guy".

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

Am I missing something simple? Any other ideas?

-西恩

推荐答案

从调用堆栈和您的描述中,我希望错误涉及远程文件.

From the callstack and your description, I would expect the error refers to the remote file.

仅在传输完成后才需要执行chmod,因此我假设SftpClient认为传输已完成,并且它尝试更新远程文件权限.似乎失败了,因为文件实际上不存在.收到错误后,请使用SftpClient.ls()检查文件是否存在.可能是您有一些远程过程,该过程会在上传完成后立即删除文件.

The chmod has to be done only after the transfer completes, so I assume the SftpClient believes the transfer is done and it tries to update the remote file permissions. And it seems like it fails, because the file is actually not there. Once you get the error, use SftpClient.ls(), to check, if the file is there. Chances are that you have some remote-side process that takes the file away the moment the upload finishes.

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

As a workaround, you can also try to prevent the SftpClient trying to modify the permissions after the upload finishes. I do not know J2SSH. Having a quick look, I have not found any API for this though. Maybe the SftpClient.umask().

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

You can try to switch to JSch. It does not seem to do implicit chmod after upload.

另外,值得检查远程服务器日志.

Also it's worth checking the remote server log.

这篇关于sshtools.SftpClient.put失败,并显示“无此文件".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆