Java SFTP 传输库 [英] Java SFTP Transfer Library
问题描述
我正在寻找一个非常简单的 Java 库来用于 SFTP 文件传输.除此之外,我不需要任何其他功能.
I'm looking for a dead simple Java Library to use for SFTP file transfers. I don't need any other features beyond that.
我试过 Zehon 的,但它令人难以置信的烦人,而且我认为 8 个 jar 文件对于我需要的这么少的功能来说有点疯狂.
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.
而且图书馆必须是免费的(就像免费啤酒一样),并且最好是开源的(不是必需的).
And the library have to be free (as in free beer), and preferable Open Source (not a requirement).
谢谢.
推荐答案
我将保留我之前的答案,因为 JSch 仍在许多地方使用,但是如果您需要更好的文档库,您可以使用 sshj.如何使用它来做 sftp 的一个例子是:
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 :
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();
}
<小时>
使用 JSch(例如 Ant 使用的 java ssh 库),您可以做一些事情像这样:
Using JSch (a java ssh lib, used by Ant for example), you could do something like that :
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();
}
}
您可以通过这种方式直接使用 JSch,或者通过 Commons VFS,但随后您将拥有同时拥有 commons vfs jar 和 jsch jar.
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.
这篇关于Java SFTP 传输库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!