java - 使用 jsch 从 SFTP 下载文件

标签 java jsch

我正在使用 JSCH 从 SFTP 服务器下载文件。 我正在使用单个 session ,通过多个 channel 从 SFTP 中的不同文件夹下载文件。 对于这个下载过程,我有一组预定的作业。每项工作将:

  1. 每次都打开一个新 channel (ChannelSftp)。 channel 名称:sftp
  2. 使用方法 ChannelSftp.ls() 获取要下载的文件总数的大小
  3. 如果 size(Vector) 大于零,则使用 ChannelSftp.get(remotedir/'*.*', localdir) 下载所有文件
  4. 最后关闭打开的 channel 。

在上述过程中,大多数时候我都遇到“未找到文件”或“没有此类文件”异常并且无法下载某些文件。

任何人都可以告诉我为什么会发生这种情况。可能是什么原因。如何解决这个问题

下面是我使用的代码:

ChannelSftp channelSftp = null;

try {
    channelSftp = getChannelConnectionUtil().openChannel(); //SFTPConnection.getSession().openChannel("sftp");  

    @SuppressWarnings("rawtypes")
    Vector numOfFiles = channelSftp.ls(ftpDir+"/*.*");

    if(numOfFiles.size() > 0){
        channelSftp.get(ftpDir+"/*.*",localDir); // Here I am getting error
    }
}  catch (Exception e) {
    e.printStackTrace();
} finally {
    getChannelConnectionUtil().disconnectChannel(channelSftp);
}

最佳答案

如果没有您的代码,则很难诊断问题。我建议忘记 vector 大小检查,只需遍历 vector 列表并计算抓取的文件数。这是我用来从远程主机检查和下载文件的代码块:

try {   
    ChannelSftp c = (ChannelSftp) channel;   
    c.lcd(localDir);
    logger.info("lcd " + c.lpwd());

    // Get a listing of the remote directory
    @SuppressWarnings("unchecked")
    Vector<ChannelSftp.LsEntry> list = c.ls("."); 
    logger.info("ls .");

    // iterate through objects in list, identifying specific file names
    for (ChannelSftp.LsEntry oListItem : list) {
        // output each item from directory listing for logs
        logger.info(oListItem.toString()); 

        // If it is a file (not a directory)
        if (!oListItem.getAttrs().isDir()) {
            // Grab the remote file ([remote filename], [local path/filename to write file to])

            logger.info("get " + oListItem.getFilename());
            c.get(oListItem.getFilename(), oListItem.getFilename());  // while testing, disable this or all of your test files will be grabbed

            grabCount++; 

            // Delete remote file
            //c.rm(oListItem.getFilename());  // Deleting remote files is not requried in every situation.
        }
    }

    // Report files grabbed to log
    if (grabCount == 0) { 
        logger.info("Found no new files to grab.");
    } else {
        logger.info("Retrieved " + grabCount + " new files.");
    }                           
} catch(SftpException e) {
    logger.warning(e.toString());
} finally {
    // disconnect session.  If this is not done, the job will hang and leave log files locked
    session.disconnect();
    logger.info("Session Closed");
}

关于java - 使用 jsch 从 SFTP 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17123990/

相关文章:

java - session 超时后显示消息

java - 如何使用 libgdx 组合 scen2d 和 OrthogonalTiledMapRenderer

java - 将Jsch放入连接池详解

java - Java中通过scp和sftp直接将文件写入远程系统的最佳方法

java - 通过Java中的SSH隧道连接到Mongo数据库

java - 用于比较数据集的任何 Java 集合

java - 构造函数 ArrayAdapter <string> 未定义

java - Android 4.2.2 上的 Drawable

java - SftpChannel.rename 失败,但 mv 命令有效。可能的原因是什么?

java - JSch, session 打开时锁定文件