使用JSch ChannelSftp:调用ChannelSftp.cd(...)和ls(..)方法时需要什么文件处理?

时间:2015-09-01 07:39:56

标签: java file io sftp jsch

下面是代码,其中通道对象初始化为: (Sftp是一个内部文件。我们假设changeDirectory()方法是这个类的一部分。)

    private void changeDirectory() throws SftpException {
    // jsch initializes
    Session session = jsch.getSession( "remote-username", "remote-host" ); 
    Channel channel = session.openChannel( "sftp" );
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    // remoteDirectory is initialized
    sftpChannel.cd(remoteDirectory);
}

执行此代码时,我收到以下格式的错误:

Caused by: 4: 
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:359)
at com.dclear.cmn.cms.net.Sftp.changeDirectory(Sftp.java:139)
at com.dclear.cmn.cms.net.Sftp.receiveFile(Sftp.java:80)
... 7 more
Caused by: java.lang.IndexOutOfBoundsException
    at java.io.PipedInputStream.read(PipedInputStream.java:372)
    at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2869)
    at com.jcraft.jsch.ChannelSftp.fill(ChannelSftp.java:2861)
    at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2319)
    at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
    ... 9 more

然而,当我深入研究它时,我明白为了避免这种IndexOutOfBoundsException,我必须注意以下事项:

len是负数,或者len大于b.length - off 其中:

b - the buffer into which the data is read.
off - the start offset in the destination array b
len - the maximum number of bytes read.

但是,这是非常内部的代码。在我的文件上调用cd(..)方法之前如何检查?我有档案位置&文件大小。

对于某些文件,我也遇到异常:

 Exception: failed to get directory listing in SFTP protocol....
4: 
    at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2187)
    at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2202)
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1566)
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1527)

代码调用时出现:

List<LsEntry> ls = sftpChannel.ls(remoteDirectory);

任何输入以避免这些随机错误?我确信它们是因为错过了一些文件处理,因此对不同的文件表现不同?

FYI jsch版本:Jsch \ 1.5 \ jsch-0.1.50.jar ChannelSftp(版本1.4:48.0,超级位)

0 个答案:

没有答案
相关问题