scp via java

前端 未结 15 1020
粉色の甜心
粉色の甜心 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:44

    I wrapped Jsch with some utility methods to make it a bit friendlier and called it

    Jscp

    Available here: https://github.com/willwarren/jscp

    SCP utility to tar a folder, zip it, and scp it somewhere, then unzip it.

    Usage:

    // create secure context
    SecureContext context = new SecureContext("userName", "localhost");
    
    // set optional security configurations.
    context.setTrustAllHosts(true);
    context.setPrivateKeyFile(new File("private/key"));
    
    // Console requires JDK 1.7
    // System.out.println("enter password:");
    // context.setPassword(System.console().readPassword());
    
    Jscp.exec(context, 
               "src/dir",
               "destination/path",
               // regex ignore list 
               Arrays.asList("logs/log[0-9]*.txt",
               "backups") 
               );
    

    Also includes useful classes - Scp and Exec, and a TarAndGzip, which work in pretty much the same way.

提交回复
热议问题