scp via java

前端 未结 15 1016
粉色の甜心
粉色の甜心 2020-11-27 13:06

What is the best method of performing an scp transfer via the Java programming language? It seems I may be able to perform this via JSSE, JSch or the bouncy castle java libr

15条回答
  •  野性不改
    2020-11-27 13:36

    I need to copy folder recursively, after trying different solutions, finally end up by ProcessBuilder + expect/spawn

    scpFile("192.168.1.1", "root","password","/tmp/1","/tmp");
    
    public void scpFile(String host, String username, String password, String src, String dest) throws Exception {
    
        String[] scpCmd = new String[]{"expect", "-c", String.format("spawn scp -r %s %s@%s:%s\n", src, username, host, dest)  +
                "expect \"?assword:\"\n" +
                String.format("send \"%s\\r\"\n", password) +
                "expect eof"};
    
        ProcessBuilder pb = new ProcessBuilder(scpCmd);
        System.out.println("Run shell command: " + Arrays.toString(scpCmd));
        Process process = pb.start();
        int errCode = process.waitFor();
        System.out.println("Echo command executed, any errors? " + (errCode == 0 ? "No" : "Yes"));
        System.out.println("Echo Output:\n" + output(process.getInputStream()));
        if(errCode != 0) throw new Exception();
    }
    

提交回复
热议问题