简体   繁体   English

使用runtime.exec发送SFTP命令

[英]Send an SFTP command by using runtime.exec

I would like to download a file from an sftp server by using the class Runtime of Java. 我想使用Java类Runtime从sftp服务器下载文件。 I can't use a library like Jsch or SSHJ because i need to use the -B option to increase the buffer size. 我不能使用Jsch或SSHJ之类的库,因为我需要使用-B选项来增加缓冲区大小。 I've tried with this code so far: 到目前为止,我已经尝试使用此代码:

public void download(String source, String destination,String port, String user, String host ){
    Runtime runtime = Runtime.getRuntime();
    String cmd = "sftp -oPort=" + port + " -B 150000 "+ user + "@" + host + ":" + source + " " + destination;

    String[] args = { "/bin/sh", "-c", cmd };
    try {
        long startTime = System.currentTimeMillis();
        log.info("cmd: "+ cmd);
        final Process process = runtime.exec(args);
        process.waitFor();
        long stopTime = System.currentTimeMillis();
        long elapsedTime = stopTime - startTime;
        System.out.println("Time: "+elapsedTime);

        BufferedReader bre = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        StringBuffer sbe = new StringBuffer();
        String lineError;
        while ((lineError = bre.readLine()) != null) {   
          sbe.append(lineError).append("\n");
        }   
        String answerError = sbe.toString();
        if(answerError.length()>0)log.error("Error:"+answerError);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

I get the error message : Connecting to xxxxxxx... with the name of the host instead of the "xxxxxx" 我收到错误消息:使用主机名而不是“ xxxxxx”连接到xxxxxxx ...

I've tried the command directly in putty and it works great but when i use it in java it doesn't work at all. 我已经在腻子中直接尝试了该命令,它的效果很好,但是当我在Java中使用它时,它根本不起作用。 I've also tried an scp command with runtime.exec() for downloading instead of an sftp and that work. 我还尝试了带有runtime.exec()的scp命令而不是sftp进行下载,并且可以工作。

Does someone have a clue why it's not working? 有人知道为什么它不起作用吗? I can't find an exemple with this method. 我找不到这种方法的例子。 Or does someone know a library for sftp transfer that allows to set the -B option? 还是有人知道允许设置-B选项的sftp传输库? i've maybe missed a method in Jsch and Sshj. 我可能已经错过了Jsch和Sshj中的方法。

Thanks in advance 提前致谢

I do this using psftp on windows. 我在Windows上使用psftp执行此操作。 I expect your results using sftp on *nix should be the same. 我希望您在* nix上使用sftp的结果应该相同。

 static java.io.File downloadFileViaSftp(String file) {
    String string = null;

    try {
        String c = "psftp -l user -P 22 -v -pw password hostname";
        Process p = Runtime.getRuntime().exec(c);

        OutputStream out = p.getOutputStream();

        Writer out2 = new BufferedWriter(new OutputStreamWriter(out));

        out2.write("get " + file + "\n");
        out2.flush();
        out2.write("close\n");
        out2.flush();
        out2.write("quit\n");
        out2.flush();
        out2.close();

        InputStream in = p.getInputStream();
        InputStream err = p.getErrorStream();

        BufferedReader bufferedreader1 = new BufferedReader(new InputStreamReader(in), 999999);
        BufferedReader bufferedreader2 = new BufferedReader(new InputStreamReader(err), 999999);

        while ((string = bufferedreader2.readLine()) != null) {
            System.out.println("2=" + string);
            System.out.flush();
        }
        bufferedreader2.close();

        while ((string = bufferedreader1.readLine()) != null) {
            System.out.println("1=" + string);
            System.out.flush();
        }
        bufferedreader1.close();

        int x = p.waitFor();
        p.destroy();

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return new java.io.File(file);

}

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

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