Caused by: com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Rea

今天使用SFTP连接服务器的时候突然报出如下问题

java.lang.RuntimeException: Sftp connect to  error
        at com.talkweb.mobilezone.publicutilitie.settling.utils.SimpleSftp.connectWithKey(SimpleSftp.java:102) ~[classes/:na]
        at com.talkweb.mobilezone.publicutilitie.xinaorq.XarqTaskImpl.loadFileFromSFTP(XarqTaskImpl.java:512) [classes/:na]
        at com.talkweb.mobilezone.publicutilitie.xinaorq.XarqTaskImpl.getFileUseFtp(XarqTaskImpl.java:182) [classes/:na]
        at com.talkweb.mobilezone.publicutilitie.xinaorq.XarqTaskImpl.execWithParams(XarqTaskImpl.java:124) [classes/:na]
        at com.talkweb.mobilezone.publicutilitie.commons.AbstractPubTaskJob.pubTask(AbstractPubTaskJob.java:34) [classes/:na]
        at com.talkweb.mobilezone.publicutilitie.xinaorq.XarqTaskImpl.execJob(XarqTaskImpl.java:605) [classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_27]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_27]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_27]
        at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_27]
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) [spring-aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) [spring-aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) [spring-aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:77) [spring-context-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [spring-aop-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at $Proxy49.execJob(Unknown Source) [na:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_27]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_27]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_27]
        at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_27]
        at com.caucho.hessian.server.HessianSkeleton.invoke(HessianSkeleton.java:306) [hessian-4.0.7.jar:na]
        at com.caucho.hessian.server.HessianSkeleton.invoke(HessianSkeleton.java:221) [hessian-4.0.7.jar:na]
        at org.springframework.remoting.caucho.HessianExporter.doInvoke(HessianExporter.java:198) [spring-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.remoting.caucho.HessianExporter.invoke(HessianExporter.java:118) [spring-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.remoting.caucho.HessianServiceExporter.handleRequest(HessianServiceExporter.java:66) [spring-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49) [spring-webmvc-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788) [spring-webmvc-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717) [spring-webmvc-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) [spring-webmvc-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) [spring-webmvc-3.0.4.RELEASE.jar:3.0.4.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) [servlet-api.jar:na]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) [servlet-api.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) [catalina.jar:7.0.70]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.70]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218) [catalina.jar:7.0.70]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.70]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) [catalina.jar:7.0.70]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) [catalina.jar:7.0.70]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) [catalina.jar:7.0.70]
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) [catalina.jar:7.0.70]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [catalina.jar:7.0.70]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442) [catalina.jar:7.0.70]
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082) [tomcat-coyote.jar:7.0.70]
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623) [tomcat-coyote.jar:7.0.70]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1756) [tomcat-coyote.jar:7.0.70]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1715) [tomcat-coyote.jar:7.0.70]
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_27]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_27]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-coyote.jar:7.0.70]
        at java.lang.Thread.run(Thread.java:662) [na:1.6.0_27]
Caused by: com.jcraft.jsch.JSchException: Session.connect: java.io.IOException: End of IO Stream Read
        at com.jcraft.jsch.Session.connect(Session.java:565) ~[jsch-0.1.54.jar:na]
        at com.jcraft.jsch.Session.connect(Session.java:183) ~[jsch-0.1.54.jar:na]
        at com.talkweb.mobilezone.publicutilitie.settling.utils.SimpleSftp.connectWithKey(SimpleSftp.java:93) ~[classes/:na]
        ... 50 common frames omitted

解决方案

1、修改服务器/etc/ssh/sshd_config配置文件,添加如下代码:

      KexAlgorithms +diffie-hellman-group1-sha1

 

2.修改sftp的代码 

JSch jsch = new JSch();
 sch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);

添加如下参数

session.setConfig("kex",“diffie-hellman-group1-sha1”);

根据提供的引用内容,你遇到的问题是`Caused by: com.jcraft.jsch.JSchException: Session.connect: java.net.SocketException: Connection reset`。这个错误通常是由于网络连接问题引起的。可能有以下几个原因导致这个错误: 1. 服务器端口未打开或被防火墙阻止:请确保你的SFTP服务器端口已正确打开,并且没有被防火墙阻止。 2. 服务器地址错误:请确保你的SFTP服务器地址是正确的,可以尝试使用ping命令来测试服务器的连通性。 3. 服务器连接超时:如果服务器连接超时,可能是由于网络延迟或服务器负载过高导致的。你可以尝试增加连接超时时间或者优化服务器性能。 4. 客户端配置错误:请确保你的Java代码中的SFTP配置是正确的,包括服务器地址、端口、用户名、密码等。 以下是一个示例代码,演示了如何使用JSch库进行SFTP上传文件: ```java import com.jcraft.jsch.*; public class SftpExample { public static void main(String[] args) { String host = "sftp.example.com"; int port = 22; String username = "your-username"; String password = "your-password"; String localFilePath = "/path/to/local/file.txt"; String remoteFilePath = "/path/to/remote/file.txt"; try { JSch jsch = new JSch(); Session session = jsch.getSession(username, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(); channel.put(localFilePath, remoteFilePath); channel.disconnect(); session.disconnect(); } catch (JSchException | SftpException e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值