首页 > 解决方案 > 通过jsch将zip文件下载到sftp站点时,输入流随机关闭错误

问题描述

我试图从另一个国家的 sftp 服务器下载 zip 文件,但有时我无法获取带有以下错误代码的 zip 文件:

at com.jcraft.jsch.ChannelSftp._get(ChannelSftp.java:1160)
    at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1006)
    at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:984)
    at com.esb.eaml.util.SFTPUtil.get(Unknown Source)
    at com.esb.eaml.job.JobKzDownloadProcessor.downloadKzFileFromLyods(Unknown Source)
    at com.esb.eaml.job.JobKzDownloadProcessor.execute(Unknown Source)
    at com.esb.eaml.job.JobKzDownloadProcessor.main(Unknown Source)
Caused by: java.io.IOException: inputstream is closed
    at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2884)
    at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2908)
    at com.jcraft.jsch.ChannelSftp._get(ChannelSftp.java:1062)
    ... 6 more

服务器所有者说他在服务器日志中没有发现任何错误。

我们使用 jsch-0.1.52.jar 进行 SFTP 连接。源代码如下所示:

    boolean isOK = false;
    FileOutputStream fos = null;
    try {
        if (initOK) {
            logger.info("Downloading file from [" + srcPath + "] to [" + savePath + "]...");
            fos = new FileOutputStream(savePath);
            channelSftp.get(srcPath, fos);
            isOK = true;
            logger.info("Download file from [" + srcPath + "] to [" + savePath + "] successful.");
        } else {
            logger.error("Download file from [" + srcPath + "] to [" + savePath
                    + "] failed, initial SFTP channel is not successful.");
        }
    } catch (FileNotFoundException e) {
        logger.error("Download file from [" + srcPath + "] to [" + savePath
                + "] failed, savePath or savePath does not exist.", e);
    } catch (SftpException e) {
        logger.error("Download file from [" + srcPath + "] to [" + savePath + "] failed.", e);
    }
    return isOK;

标签: sftpinputstreamjsch

解决方案


推荐阅读