JSCH:没有这样的文件错误

JSCH: No such file error

我有 windows 台带有 运行 ssh 服务器的机器。我知道那台机器上的路径。让它成为 D:/Folder1/Folder2。我正在使用 JSCH 创建 sftp 通道。但是当我尝试 cd 到 D:/Folder1/Folder2 时,会抛出 "No such file: 2" 错误。

有人可以帮忙吗?可能我需要转换路径?

我已经通过打开 exec 通道使用 ChannelExec 解决了这个问题。这对我有用。希望对其他人也有用。

    ...
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");

    JSch ssh = new JSch();
    session = ssh.getSession(sshSolrUsername, sshSolrHost, 22);
    session.setConfig(config);
    session.setPassword(sshSolrPassword);
    session.connect();
    channel = session.openChannel("exec");        
    ((ChannelExec)channel ).setCommand("cd " + sourcePath);     
    exec.setInputStream(null);
    ((ChannelExec)channel ).setErrStream(System.err);
    InputStream in = channel .getInputStream();
    channel .connect();
    int status = checkStatus(channel , in);
    channel.disconnect();
    ...

问题出在路径上。它没有找到要重命名的文件,如果您使用的是 JSCH 库,则可以使用 channelSftp.pwd () 知道当前位置。

示例:

Sftp sftp = new Sftp();
sftp.conectar();
ChannelSftp channelSftp = sftp.getChannelSftp();
channelSftp.rename(channelSftp.pwd()+"//"+file,`channelSftp.pwd()`+"//"+"new_dir//"+file);
//or
String url = channelSftp.pwd();
String url_m = channelSftp.pwd()+"/"+directory; 
channelSftp.rename(url+file,url_m+file)



 

我建议你在inpuStream中读取你的文件,然后使用函数:

        put(InputStream src, String dst)

文档:https://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/ChannelSftp.html

示例:

        String fileName = "D:/Folder1/Folder2";
        File initialFile = new File(fileName);
        InputStream targetStream = new FileInputStream(initialFile);
        channelSftp.put(targetStream, "./in/");