我用一些实用方法包装了Jsch,使它更友好并称之为它
可在此处获取:https://github.com/willwarren/jscp
SCP实用程序tar一个文件夹,压缩它,并将其scp到某处,然后解压缩它.
用法:
// 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")
);
Run Code Online (Sandbox Code Playgroud)
还包括有用的类 - Scp和Exec,以及TarAndGzip,它们的工作方式几乎相同.
这是高级解决方案,无需重新发明。又快又脏!
1)首先,转到http://ant.apache.org/bindownload.cgi并下载最新的Apache Ant二进制文件。(如今,apache-ant-1.9.4-bin.zip)。
2)解压缩下载的文件并找到JAR ant-jsch.jar(“ apache-ant-1.9.4 / lib / ant-jsch.jar”)。在您的项目中添加此JAR。也是ant-launcher.jar和ant.jar。
3)转到Jcraft jsch SouceForge项目并下载jar。如今, jsch-0.1.52.jar。还要在您的项目中添加此JAR。
现在,您可以轻松地在Java代码中使用Ant类Scp来通过网络复制文件或 SSHExec用于SSH服务器中的命令,轻松地用于。
4)代码示例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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
93560 次 |
| 最近记录: |