scp via java

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

    This is high-level solution, no need to reinvent. Quick and dirty!

    1) First, go to http://ant.apache.org/bindownload.cgi and download latest Apache Ant binary. (nowadays, apache-ant-1.9.4-bin.zip).

    2) Extract the downloaded file and find the JAR ant-jsch.jar ("apache-ant-1.9.4/lib/ant-jsch.jar"). Add this JAR in your project. Also ant-launcher.jar and ant.jar.

    3) Go to Jcraft jsch SouceForge Project and download the jar. Nowadays, jsch-0.1.52.jar. Also Add this JAR in your project.

    Now, can you easyly use into java code the Ant Classes Scp for copy files over network or SSHExec for commands in SSH servers.

    4) Code Example Scp:

    // This make scp copy of 
    // one local file to remote dir
    
    org.apache.tools.ant.taskdefs.optional.ssh.Scp scp = new Scp();
    int portSSH = 22;
    String srvrSSH = "ssh.your.domain";
    String userSSH = "anyuser"; 
    String pswdSSH = new String ( jPasswordField1.getPassword() );
    String localFile = "C:\\localfile.txt";
    String remoteDir = "/uploads/";
    
    scp.setPort( portSSH );
    scp.setLocalFile( localFile );
    scp.setTodir( userSSH + ":" + pswdSSH + "@" + srvrSSH + ":" + remoteDir );
    scp.setProject( new Project() );
    scp.setTrust( true );
    scp.execute();
    

提交回复
热议问题