Java SFTP 传输库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2346764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java SFTP Transfer Library
提问by Claus J?rgensen
I'm looking for a dead simple Java Library to use for SFTP file transfers. I don't need any other features beyond that.
我正在寻找一个简单的 Java 库来用于 SFTP 文件传输。除此之外,我不需要任何其他功能。
I've tried Zehon's, but it's incredible naggy, and I think 8 jar files is a bit crazy for so little functionality as I require.
我试过 Zehon 的,但它令人难以置信的烦人,而且我认为 8 个 jar 文件对于我需要的功能如此之少有点疯狂。
And the library have to be free (as in free beer), and preferable Open Source (not a requirement).
并且图书馆必须是免费的(如免费啤酒),并且最好是开源的(不是必需的)。
Thanks.
谢谢。
采纳答案by Valentin Rocher
Edit :I'm going to keep my previous answer, as JSch is still used in many places, but if you need a better-documented library, you can use sshj. An example in how to use it to do sftp is :
编辑:我将保留我之前的答案,因为 JSch 仍在许多地方使用,但是如果您需要一个更好的文档库,您可以使用sshj。如何使用它来做 sftp 的一个例子是:
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("host");
try {
ssh.authPassword("username", "password");
SFTPClient sftp = ssh.newSFTPClient();
try {
sftp.put(new FileSystemFile("/path/of/local/file"), "/path/of/ftp/file");
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}
Using JSch(a java ssh lib, used by Ant for example), you could do something like that :
使用JSch(例如 Ant 使用的 java ssh 库),您可以执行以下操作:
Session session = null;
Channel channel = null;
try {
JSch ssh = new JSch();
ssh.setKnownHosts("/path/of/known_hosts/file");
session = ssh.getSession("username", "host", 22);
session.setPassword("password");
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
sftp.put("/path/of/local/file", "/path/of/ftp/file");
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
You can use JSch directly this way, or through Commons VFS, but then you'll have to have both commons vfs jar and jsch jar.
您可以通过这种方式直接使用 JSch,也可以通过Commons VFS 使用,但是您必须同时拥有 commons vfs jar 和 jsch jar。
回答by Konrad Garus
How about FTPSClientfrom Apache Commons?
来自 Apache Commons 的FTPSClient怎么样?
回答by Iraklis
Here is the complete source code of an example using JSchwithout having to worry about the ssh key checking.
这是使用JSch的示例的完整源代码,无需担心 ssh 密钥检查。
import com.jcraft.jsch.*;
public class TestJSch {
public static void main(String args[]) {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession("username", "127.0.0.1", 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword("password");
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.get("remotefile.txt", "localfile.txt");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SftpException e) {
e.printStackTrace();
}
}
}
回答by tg1
The exit() and the disconnect() must be placed in a finally block. In this example we've got a never ending program in case of an exception because a second thread won't terminate.
exit() 和 disconnect() 必须放在 finally 块中。在这个例子中,我们有一个永不结束的程序,以防出现异常,因为第二个线程不会终止。