try { JSch jsch = new JSch(); Logger log = new com.jcraft.jsch.Logger() { public boolean isEnabled(int i) { // 开启、关闭调试 return true; } public void log(int i, String s) { // 打印日志 // ItpUtil.log(s); } }; JSch.setLogger(log); session = jsch.getSession(user, host, this.port); session.setConfig("PreferredAuthentications", "password"); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(psw); session.setTimeout(60000); session.setServerAliveInterval(2000);//这里必须要设置长点 session.connect(connTime);//这里也要设置时间 } catch (Exception e) { throw new RuntimeException(e); } channel = session.openChannel(SHELL); PrintWriter pw = new PrintWriter(channel.getOutputStream()); channel.connect(2000);//最最重要的地方 这里也是必须要设置时间的!!这里就是一连接上就断的祸首。 //channel.connect();//使用这方法的时候 代码内居然是设置0毫秒。也就是说服务器稍微反应慢点就断了。