Jsch文件上传被取消

时间:2018-10-04 06:34:53

标签: java upload sftp jsch

我想上传一个大文件,例如SFTP服务器。我可以成功连接,并且上传较小的文件不是问题。我要上传大于150MB的文件时,上传失败。

我不知道为什么会这样。如果我使用FileZilla之类的东西将同一文件传输到同一服务器,则一切正常。所以我想问题是我的Java实现。

发生这种情况时,我收到“ Inputstream已关闭”的异常:

main()

这是我的代码:

4: java.io.IOException: inputstream is closed
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:697)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:475)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:365)
at de.laudert.contentflow.client.service.data.impl.storageclient.impl.FTPStorageUploadClient.upload(FTPStorageUploadClient.java:45)
at de.laudert.contentflow.client.service.data.impl.StorageUploadServiceBean.uploadPictureToStorage(StorageUploadServiceBean.java:86)
at de.laudert.contentflow.client.service.background.impl.StorageUploadStartServiceBean.startUploadInternal(StorageUploadStartServiceBean.java:139)
at de.laudert.contentflow.client.service.background.impl.StorageUploadStartServiceBean.lambda$startUploadForAllFiles$1(StorageUploadStartServiceBean.java:99)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: inputstream is closed
    at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2911)
    at com.jcraft.jsch.ChannelSftp.header(ChannelSftp.java:2935)
    at com.jcraft.jsch.ChannelSftp.checkStatus(ChannelSftp.java:2473)
    at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:651)
    ... 7 more

这是我的Jsch日志

public void upload(StorageCredentials credentials, String uploadFileName, String uploadVolumeName, BufferedInputStream bis, String localFilePath, String localFileSize) throws Exception {
    JSch jsch = new JSch();
    Session session = null;
    try {
        session = jsch.getSession(credentials.getAccessKey(), credentials.getEndPointURL(), 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(credentials.getSecretKey());
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        sftpChannel.put(localFilePath, "/" + PATH + uploadFileName);
        sftpChannel.exit();
        session.disconnect();
    }
    catch (JSchException e) {
        e.printStackTrace();
    }
    catch (SftpException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

Session的默认超时应为“ 0”(无限),但是有时OS会覆盖它。

尝试通过setTimeout()手动设置超时;

public void upload(StorageCredentials credentials, String uploadFileName, String uploadVolumeName, BufferedInputStream bis, String localFilePath, String localFileSize) throws Exception {
    JSch jsch = new JSch();
    Session session = null;
    try {
        session = jsch.getSession(credentials.getAccessKey(), credentials.getEndPointURL(), 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(credentials.getSecretKey());
        session.connect(300000); // 5 mins

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;
        sftpChannel.put(localFilePath, "/" + PATH + uploadFileName);
        sftpChannel.exit();
        session.disconnect();
    }
    catch (JSchException e) {
        e.printStackTrace();
    }
    catch (SftpException e) {
        e.printStackTrace();
    }
}