Jsch File-Upload отменяется - PullRequest
       19

Jsch File-Upload отменяется

0 голосов
/ 04 октября 2018

Я хочу загрузить большой файл, так что SFTP-сервер.Я могу подключиться успешно, и загрузка файлов меньшего размера не является проблемой.Как только я захочу загрузить файлы размером более 150 МБ, загрузка завершится неудачно.

Я не знаю, почему это происходит.Если я использую что-то вроде FileZilla для передачи того же файла на тот же сервер, все в порядке.Так что я думаю, что проблема в моей Java-реализации.

Когда это происходит, я получаю "Inputstream closed" -Exception:

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

Вот мой код:

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();
    }
}

Вот мой журнал Jsch

Connecting to <Replaced> port 22
Connection established
Remote version string: SSH-2.0-WingFTPServer
Local version string: SSH-2.0-JSCH-0.1.54
CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
SSH_MSG_KEXINIT sent
SSH_MSG_KEXINIT received
kex: server: curve25519-sha256@libssh.org,ecdh-sha2-nistp256,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
kex: server: ssh-rsa
kex: server: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,blowfish-cbc,3des-cbc,des-cbc-ssh1
kex: server: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,blowfish-cbc,3des-cbc,des-cbc-ssh1
kex: server: hmac-sha2-256,hmac-sha2-512,hmac-sha1
kex: server: hmac-sha2-256,hmac-sha2-512,hmac-sha1
kex: server: none,zlib,zlib@openssh.com
kex: server: none,zlib,zlib@openssh.com
kex: server: 
kex: server: 
kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-ctr,aes192-cbc,aes256-ctr,aes256-cbc
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: none
kex: client: none
kex: client: 
kex: client: 
kex: server->client aes128-ctr hmac-sha1 none
kex: client->server aes128-ctr hmac-sha1 none
SSH_MSG_KEX_ECDH_INIT sent
expecting SSH_MSG_KEX_ECDH_REPLY
ssh_rsa_verify: signature true
Permanently added '<Replaced>' (RSA) to the list of known hosts.
SSH_MSG_NEWKEYS sent
SSH_MSG_NEWKEYS received
SSH_MSG_SERVICE_REQUEST sent
SSH_MSG_SERVICE_ACCEPT received
Authentications that can continue: publickey,keyboard-interactive,password
Next authentication method: publickey
Authentications that can continue: password
Next authentication method: password
Authentication succeeded (password).
Caught an exception, leaving main loop due to Connection reset
Disconnecting from <Replaced> port 22

1 Ответ

0 голосов
/ 04 октября 2018

Время ожидания по умолчанию Session должно быть равно «0» (бесконечно), но иногда оно переопределяется ОС.

Попробуйте вручную установить время ожидания с помощью 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();
    }
}
...