我想上传一个大文件,所以是一个 SFTP 服务器。我可以成功连接并且上传较小的文件不是问题。一旦我想上传大于150MB的文件,上传就会失败。
我不知道为什么会发生这种情况。如果我使用 FileZilla 之类的工具将相同的文件传输到同一服务器,则一切正常。所以我猜问题是我的 Java 实现。
发生这种情况时,我会收到“Inputstream closeed”异常:
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
最佳答案
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();
}
}
关于java - Jsch 文件上传已取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52640477/