java - 在 JSch ChannelSftp 操作上配置超时

标签 java sftp jsch

我正在使用 JSch 库从 SFTP 服务器列出和下载文件。

Channel channel = this.session.openChannel(SFTP_CHANNEL_NAME);
channel.connect();
sftpChannel = (ChannelSftp) channel;
Vector<LsEntry> listing = sftpChannel.ls("*");
打电话时ls ,应用程序线程有时会卡住。
线程转储 -
Thread 15108: (state = BLOCKED)
java.lang.Object.wait(long) @bci=0 (Compiled frame; information may be imprecise)
java.io.PipedInputStream.read() @bci=142, line=310 (Compiled frame)
java.io.PipedInputStream.read(byte[], int, int) @bci=43, line=361 (Compiled frame)
com.jcraft.jsch.ChannelSftp.fill(byte[], int, int) @bci=17, line=2527 (Compiled frame)
com.jcraft.jsch.ChannelSftp.header(com.jcraft.jsch.Buffer, com.jcraft.jsch.ChannelSftp$Header) @bci=12, line=2553 (Interpreted frame)
com.jcraft.jsch.ChannelSftp.ls(java.lang.String) @bci=298, line=1424 (Interpreted frame)
有没有办法在 ls 上配置超时和其他方法?我在 channel.connect(timeout) 上看到设置超时但这只会在连接到远程服务器时设置超时。

最佳答案

防止命令卡住的正确方法是在 session 上设置 serverAliveInterval。从源代码:

  /**
   * Sets the interval to send a keep-alive message.  If zero is
   * specified, any keep-alive message must not be sent.  The default interval
   * is zero.
   * @param interval the specified interval, in milliseconds.
   * @see #getServerAliveInterval()
   */
  public void setServerAliveInterval(int interval) throws JSchException {
    setTimeout(interval);
    this.serverAliveInterval=interval;
  }

关于java - 在 JSch ChannelSftp 操作上配置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16546198/

相关文章:

java - 如何找到失败的 checkstyle 规则的名称

java - 运行 docx4j createhtml.java 文件时出错

java - SFTP服务器多用户认证

java - 我怎样才能使 JSch 中的 session 持续更长时间?

android - JSch:如何使用 ssh-keys ssh 进入服务器

java.lang.NoClassDefFoundError : com/jcraft/jsch/JSch error in JSP file

java - 从日期中仅获取月份并在 java 中给出月份名称

java - POI 3.2 图像高度/宽度控制

php - 使用 SFTP 上传文件

linux - CentOS 权限、文件夹、用户和群组