我正在使用 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/