通过Java编程语言执行scp传输的最佳方法是什么?看来我可以通过JSSE,JSch或充气城堡Java库执行此操作。这些解决方案似乎都没有一个简单的答案。
Answers:
我最终使用了Jsch-这非常简单,而且似乎可以很好地扩展(我每隔几分钟就抓取几千个文件)。
我用一些实用程序方法包装了Jsch,使其变得更加友好,并称之为
在这里可用:https : //github.com/willwarren/jscp
SCP实用程序可将文件夹压缩,压缩并在某个位置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")
);
还包括有用的类-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();
在OpenSSH的项目列出几个Java的替代品,三铅SSH的Java似乎符合你要求的。
我使用的SFTP API具有称为Zehon的SCP,它很棒,非常容易使用,并带有许多示例代码。这是网站http://www.zehon.com
我看了很多这些解决方案,但不喜欢其中的许多。主要是因为必须识别您的已知主机的烦人的步骤。相对于scp命令,JSCH和JSCH处于荒谬的低水平。
我找到了一个不需要此库的库,但将其打包并用作命令行工具。 https://code.google.com/p/scp-java-client/
我浏览了源代码,发现如何在不使用命令行的情况下使用它。这是一个上传示例:
uk.co.marcoratto.scp.SCP scp = new uk.co.marcoratto.scp.SCP(new uk.co.marcoratto.scp.listeners.SCPListenerPrintStream());
scp.setUsername("root");
scp.setPassword("blah");
scp.setTrust(true);
scp.setFromUri(file.getAbsolutePath());
scp.setToUri("root@host:/path/on/remote");
scp.execute();
最大的缺点是它不在Maven仓库中(我可以找到)。但是,易用性对我来说值得。
jsCH对我来说很棒。下面是一个方法示例,该方法将连接到sftp服务器并将文件下载到指定目录。建议不要禁用StrictHostKeyChecking。尽管设置起来有些困难,但是出于安全原因,应该指定已知主机。
jsch.setKnownHosts(“ C:\ Users \ test \ known_hosts”); 推荐的
JSch.setConfig(“ StrictHostKeyChecking”,“ no”); - 不建议
import com.jcraft.jsch.*;
public void downloadFtp(String userName, String password, String host, int port, String path) {
Session session = null;
Channel channel = null;
try {
JSch ssh = new JSch();
JSch.setConfig("StrictHostKeyChecking", "no");
session = ssh.getSession(userName, host, port);
session.setPassword(password);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
sftp.get(path, "specify path to where you want the files to be output");
} catch (JSchException e) {
System.out.println(userName);
e.printStackTrace();
} catch (SftpException e) {
System.out.println(userName);
e.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
像这里的一些人一样,我最终围绕JSch库编写了一个包装器。
它称为way-secshell,托管在GitHub上:
https://github.com/objectos/way-secshell
// scp myfile.txt localhost:/tmp
File file = new File("myfile.txt");
Scp res = WaySSH.scp()
.file(file)
.toHost("localhost")
.at("/tmp")
.send();
JSch是一个不错的库。对于您的问题,答案很简单。
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking","no");
session.setConfig(config);
session.connect();
boolean ptimestamp = true;
// exec 'scp -t rfile' remotely
String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out=channel.getOutputStream();
InputStream in=channel.getInputStream();
channel.connect();
if(checkAck(in)!=0){
System.exit(0);
}
File _lfile = new File(lfile);
if(ptimestamp){
command="T "+(_lfile.lastModified()/1000)+" 0";
// The access time should be sent here,
// but it is not accessible with JavaAPI ;-<
command+=(" "+(_lfile.lastModified()/1000)+" 0\n");
out.write(command.getBytes()); out.flush();
if(checkAck(in)!=0){
System.exit(0);
}
}
您可以在以下位置找到完整的代码
http://faisalbhagat.blogspot.com/2013/09/java-uploading-file-remotely-via-scp.html
这是一个使用JSch上传文件的示例:
ScpUploader.java
:
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.ByteArrayInputStream;
import java.util.Properties;
public final class ScpUploader
{
public static ScpUploader newInstance()
{
return new ScpUploader();
}
private volatile Session session;
private volatile ChannelSftp channel;
private ScpUploader(){}
public synchronized void connect(String host, int port, String username, String password) throws JSchException
{
JSch jsch = new JSch();
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig(config);
session.setInputStream(System.in);
session.connect();
channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
}
public synchronized void uploadFile(String directoryPath, String fileName, byte[] fileBytes, boolean overwrite) throws SftpException
{
if(session == null || channel == null)
{
System.err.println("No open session!");
return;
}
// a workaround to check if the directory exists. Otherwise, create it
channel.cd("/");
String[] directories = directoryPath.split("/");
for(String directory : directories)
{
if(directory.length() > 0)
{
try
{
channel.cd(directory);
}
catch(SftpException e)
{
// swallowed exception
System.out.println("The directory (" + directory + ") seems to be not exist. We will try to create it.");
try
{
channel.mkdir(directory);
channel.cd(directory);
System.out.println("The directory (" + directory + ") is created successfully!");
}
catch(SftpException e1)
{
System.err.println("The directory (" + directory + ") is failed to be created!");
e1.printStackTrace();
return;
}
}
}
}
channel.put(new ByteArrayInputStream(fileBytes), directoryPath + "/" + fileName, overwrite ? ChannelSftp.OVERWRITE : ChannelSftp.RESUME);
}
public synchronized void disconnect()
{
if(session == null || channel == null)
{
System.err.println("No open session!");
return;
}
channel.exit();
channel.disconnect();
session.disconnect();
channel = null;
session = null;
}
}
AppEntryPoint.java
:
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public final class AppEntryPoint
{
private static final String HOST = "192.168.1.1";
private static final int PORT = 22;
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public static void main(String[] args) throws IOException
{
ScpUploader scpUploader = ScpUploader.newInstance();
try
{
scpUploader.connect(HOST, PORT, USERNAME, PASSWORD);
}
catch(JSchException e)
{
System.err.println("Failed to connect the server!");
e.printStackTrace();
return;
}
System.out.println("Successfully connected to the server!");
byte[] fileBytes = Files.readAllBytes(Paths.get("C:/file.zip"));
try
{
scpUploader.uploadFile("/test/files", "file.zip", fileBytes, true); // if overwrite == false, it won't throw exception if the file exists
System.out.println("Successfully uploaded the file!");
}
catch(SftpException e)
{
System.err.println("Failed to upload the file!");
e.printStackTrace();
}
scpUploader.disconnect();
}
}
我写了一个scp服务器,它比其他服务器容易得多。我使用Apache MINA项目(Apache SSHD)进行开发。您可以在这里查看:https : //github.com/boomz/JSCP
您也可以从/jar
目录下载jar文件。如何使用?看一下:https : //github.com/boomz/JSCP/blob/master/src/Main.java
在尝试了不同的解决方案之后,我需要递归地复制文件夹,最后由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();
}
-:如果您使用Maven进行依赖管理,请稍微完善Fernando的答案:-
pom.xml:
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>${ant-jsch.version}</version>
</dependency>
在您的项目中添加此依赖项。最新版本可以在这里找到。
Java代码:
public void scpUpload(String source, String destination) {
Scp scp = new Scp();
scp.setPort(port);
scp.setLocalFile(source);
scp.setTodir(username + ":" + password + "@" + host + ":" + destination);
scp.setProject(new Project());
scp.setTrust(true);
scp.execute();
}