我试图使用Java使用SFTP(而不是FTPS)从服务器检索一个文件。我该怎么做呢?


当前回答

有3个成熟的用于SFTP的Java库:Commons VFS、SSHJ和JSch

总而言之,如果你不需要Commons VFS提供的其他存储支持,那么SSHJ具有最清晰的API,是其中最好的API。

下面是从github编辑的SSHJ示例:

final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts(); // or, to skip host verification: ssh.addHostKeyVerifier(new PromiscuousVerifier())
ssh.connect("localhost");
try {
    ssh.authPassword("user", "password"); // or ssh.authPublickey(System.getProperty("user.name"))
    final SFTPClient sftp = ssh.newSFTPClient();
    try {
        sftp.get("test_file", "/tmp/test.tmp");
    } finally {
        sftp.close();
    }
} finally {
    ssh.disconnect();
}

其他回答

我使用这个叫做Zehon的SFTP API,它很棒,很容易使用,有很多示例代码。以下是http://www.zehon.com网站

你也有JFileUpload SFTP插件(Java也是): http://www.jfileupload.com/products/sftp/index.html

安迪,要在远程系统上删除文件,你需要使用JSch的(channelExec)并传递unix/linux命令来删除它。

另一种选择是考虑查看JSch库。JSch似乎是一些大型开源项目的首选库,包括Eclipse、Ant和Apache Commons HttpClient等等。

它很好地支持用户/通行证和基于证书的登录,以及所有其他漂亮的SSH2特性。

这是一个简单的远程文件检索SFTP。错误处理留给读者练习:-)

JSch jsch = new JSch();

String knownHostsFilename = "/home/username/.ssh/known_hosts";
jsch.setKnownHosts( knownHostsFilename );

Session session = jsch.getSession( "remote-username", "remote-host" );    
{
  // "interactive" version
  // can selectively update specified known_hosts file 
  // need to implement UserInfo interface
  // MyUserInfo is a swing implementation provided in 
  //  examples/Sftp.java in the JSch dist
  UserInfo ui = new MyUserInfo();
  session.setUserInfo(ui);

  // OR non-interactive version. Relies in host key being in known-hosts file
  session.setPassword( "remote-password" );
}

session.connect();

Channel channel = session.openChannel( "sftp" );
channel.connect();

ChannelSftp sftpChannel = (ChannelSftp) channel;

sftpChannel.get("remote-file", "local-file" );
// OR
InputStream in = sftpChannel.get( "remote-file" );
  // process inputstream as needed

sftpChannel.exit();
session.disconnect();

JSch库是一个功能强大的库,可以用来从SFTP服务器读取文件。下面是逐行从SFTP位置读取文件的测试代码

JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession("user", "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;

            InputStream stream = sftpChannel.get("/usr/home/testfile.txt");
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(stream));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }

            } catch (IOException io) {
                System.out.println("Exception occurred during reading file from SFTP server due to " + io.getMessage());
                io.getMessage();

            } catch (Exception e) {
                System.out.println("Exception occurred during reading file from SFTP server due to " + e.getMessage());
                e.getMessage();

            }

            sftpChannel.exit();
            session.disconnect();
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        }

整个节目请参考博客。