空指针从远程复制文件时出现异常

时间:2016-06-15 23:16:20

标签: java jsch

此问题与Zon发布的以下答案有关。

Transfer folder and subfolders using channelsftp in JSch?

我使用以下代码将文件夹从远程复制到本地。

public void downloadDir(String sourcePath, String destPath) throws SftpException { // With subfolders and all files.
    // Create local folders if absent.
    try {
        new File(destPath).mkdirs();
    } catch (Exception e) {
        System.out.println("Error at : " + destPath);
    }
    sftpChannel.lcd(destPath);

    // Copy remote folders one by one.
    lsFolderCopy(sourcePath, destPath); // Separated because loops itself inside for subfolders.
}

private void lsFolderCopy(String sourcePath, String destPath) throws SftpException { // List source (remote, sftp) directory and create a local copy of it - method for every single directory.
    Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(sourcePath); // List source directory structure.
    for (ChannelSftp.LsEntry oListItem : list) { // Iterate objects in the list to get file/folder names.
        if (!oListItem.getAttrs().isDir()) { // If it is a file (not a directory).
            if (!(new File(destPath + "/" + oListItem.getFilename())).exists() || (oListItem.getAttrs().getMTime() > Long.valueOf(new File(destPath + "/" + oListItem.getFilename()).lastModified() / (long) 1000).intValue())) { // Download only if changed later.
                new File(destPath + "/" + oListItem.getFilename());
                sftpChannel.get(sourcePath + "/" + oListItem.getFilename(), destPath + "/" + oListItem.getFilename()); // Grab file from source ([source filename], [destination filename]).
            }
        } else if (!".".equals(oListItem.getFilename() || "..".equals(oListItem.getFilename())) {
            new File(destPath + "/" + oListItem.getFilename()).mkdirs(); // Empty folder copy.
            lsFolderCopy(sourcePath + "/" + oListItem.getFilename(), destPath + "/" + oListItem.getFilename()); // Enter found folder on server to read its contents and create locally.
        }
    }
}

但是我在以下行得到一个空指针异常,我无法解决。

Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(sourcePath); // List source directory structure

我知道读取源路径有问题,但为什么?请建议。

编辑:

这是堆栈跟踪:

Exception in thread "main" 4: 
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1720)
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1526)
    at org.gradle.CopyTest.downloadDir(CopyTest.java:68)
    at org.gradle.CopyTest.main(CopyTest.java:97)
Caused by: java.lang.NullPointerException
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1543)
    ... 3 more

此异常被抛出:

         throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);

我相信这句话:

((MyPipedInputStream)io_in).updateReadSide();

0 个答案:

没有答案