使用jsch将大文件上传到sftp服务器失败,并出现java.net.SocketException:短时间内连接重置异常

时间:2018-12-21 16:06:42

标签: java sftp

im尝试使用Java jsch lib将大文件(2.5GB)上传到sftp服务器。在短暂的异常后,用stacktrace抛出:

Exception in thread "main" 4: java.net.SocketException: Connection reset
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:689)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:540)
at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:492)
at com.pcg.app.RunPull.uploadFile(RunPull.scala:104)
at com.pcg.app.RunPull.generateFile(RunPull.scala:65)
at com.pcg.app.RunPull.$anonfun$new$1(RunPull.scala:27)
                  at com.pcg.app.RunPull.$anonfun$new$1$adapted(RunPull.scala:23)
at scala.collection.immutable.List.foreach(List.scala:389)
at com.pcg.app.RunPull.<init>(RunPull.scala:23)
at com.pcg.app.RunPull$.delayedEndpoint$com$pcg$app$RunPull$1(RunPull.scala:117)
at com.pcg.app.RunPull$delayedInit$body.apply(RunPull.scala:115)
at scala.Function0.apply$mcV$sp(Function0.scala:34)
at scala.Function0.apply$mcV$sp$(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
                  at scala.collection.immutable.List.foreach(List.scala:389)
                  at scala.App.main(App.scala:76)
                  at scala.App.main$(App.scala:74)
at com.pcg.app.RunPull$.main(RunPull.scala:115)
at com.pcg.app.RunPull.main(RunPull.scala)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:115)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at com.jcraft.jsch.IO.put(IO.java:60)
at com.jcraft.jsch.Session._write(Session.java:1354)
at com.jcraft.jsch.Session.write(Session.java:1321)
at com.jcraft.jsch.ChannelSftp.sendWRITE(ChannelSftp.java:2606)
at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:669)

这是我的代码:

    val jsch =  new JSch
val session = jsch.getSession("user", "host", 8442)
session.setConfig("StrictHostKeyChecking", "no")
session.setTimeout(60000)
session.setServerAliveInterval(3000)

try {
  session.setPassword("pass")
  session.connect()
  val channel = session.openChannel("sftp")
  channel.connect()
  val sftpChannel = channel.asInstanceOf[ChannelSftp]
  if (!sftpChannel.isConnected) println("Failed to open connection to SFTP")
  sftpChannel.cd("folder/folder2")
  val f = new File(file)

  sftpChannel.put(new FileInputStream(f),f.getName)
  sftpChannel.exit()
} finally if (session != null) if (session.isConnected) session.disconnect()

请注意,从命令行(ubuntu)使用sftp客户端上载相同的文件时,文件已使用相同的用户名/密码和服务器成功上载。 该代码也适用于较小的文件。

我需要为大文件进行设置吗?

0 个答案:

没有答案