jsch channel.java 292_java - 为什么JSch ChannelSftp返回“句柄关闭”? - 堆栈内存溢出...

我正在使用jsch 0.1.54,并且正在尝试通过SFTP访问FTP服务器。 一切正常,我可以转到所需的目录,但是当我使用执行channel.ls(“ *”);的模式获取文件时; 我只有第一个匹配项,而下一个匹配项是“句柄关闭 ”,但没有文件名。 为什么?

更确切地说,当我最终到达所需目录时,

sftp.ls(ftpAccount + File.separator + lsEntry.getFilename() +

File.separator + "*", new ChannelSftp.LsEntrySelector() {...});

也许LsEntrySelectore是“句柄关闭”的原因!

这是该方法的完整代码:

public Map readFiles () {

final Map vaFiles = new HashMap();

final Channel channel;

try {

channel = session.openChannel("sftp");

channel.connect();

final List ftpAccounts = Arrays.stream(this.ftpAccounts).collect(Collectors.toList());

for (final String ftpAccount : ftpAccounts) {

System.out.println(ftpAccount);

try {

try {

final SftpATTRS attrs = sftp.stat(ftpAccount + File.separator + XXX_DIR);

} catch (Exception e) {

// The folder ftpAccount + File.separator + XXX_DIR does not exist.

// Simply continue...

continue;

}

final Vector files = sftp.ls(ftpAccount + File.separator + "*");

final List ftpFileList = files.stream().collect(Collectors.toList());

for (final ChannelSftp.LsEntry lsEntry : ftpFileList) {

if (lsEntry.getAttrs().isDir() &&

lsEntry.getFilename().equals(XXX_DIR)) {

sftp.ls(ftpAccount + File.separator + lsEntry.getFilename() +

File.separator + "*", new ChannelSftp.LsEntrySelector() {

@Override

public int select (final ChannelSftp.LsEntry entry) {

final Matcher mtc = pattern.matcher(entry.getFilename());

final SftpATTRS attr = entry.getAttrs();

if (mtc.find() && !attr.isDir() && !attr.isLink()) {

System.out.println(entry.getFilename());

try {

final ByteArrayOutputStream baos = new

ByteArrayOutputStream();

sftp.get(ftpAccount + File.separator +

XXX_DIR + File.separator + entry.getFilename(), baos);

vaFiles.put(ftpAccount + File.separator +

XXX_DIR + File.separator + entry.getFilename(),

baos.toByteArray());

logger.info("File " + ftpAccount + File.separator +

XXX_DIR + File.separator + entry.getFilename() + " downloaded.");

baos.close();

} catch (IOException e) {

e.printStackTrace();

} catch (SftpException e) {

e.printStackTrace();

}

}

System.out.println("CONTINUE = " + CONTINUE);

return CONTINUE;

}

});

}//end if

}

} catch (SftpException e) {

e.printStackTrace();

}

break;

}

} catch (JSchException e) {

e.printStackTrace();

}

return vaFiles;

}

这是我打开和关闭ByteArrayOutputStream的方式吗?

例外:

4:

at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1747)

at de.postcon.SftpFileHandler.readFiles(SftpFileHandler.java:150)

at de.postcon.VaFtp2EmMover.downloadVAFiles(VaFtp2EmMover.java:145)

at de.postcon.VaFtp2EmMover.main(VaFtp2EmMover.java:84)

Caused by: java.lang.ArrayIndexOutOfBoundsException

at java.lang.System.arraycopy(Native Method)

at com.jcraft.jsch.Buffer.getByte(Buffer.java:148)

at com.jcraft.jsch.Buffer.getString(Buffer.java:188)

at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1675)

... 3 more

将此方法的代码减至最少:

public Map readFiles () {

final Map vaFiles = new HashMap();

final String ftpAccount = "/accumio";

try {

sftp.ls(ftpAccount + File.separator + TO_POSTCON_DIR + File.separator + "*", new ChannelSftp.LsEntrySelector() {

@Override

public int select (final ChannelSftp.LsEntry entry) {

final Matcher mtc = pattern.matcher(entry.getFilename());

final SftpATTRS attr = entry.getAttrs();

if (mtc.find() && !attr.isDir() && !attr.isLink()) {

System.out.println(entry.getFilename());

}

System.out.println("CONTINUE = " + CONTINUE);

return CONTINUE;

}

});

} catch (SftpException e) {

e.printStackTrace();

}

return vaFiles;

}

...正在工作。 因此,问题似乎是由sftp.get(...)产生的; 因为当我把这条线

final ByteArrayOutputStream baos = new ByteArrayOutputStream();

sftp.get(ftpAccount + File.separator + XXX_DIR + File.separator + entry.getFilename(), baos);

在System.out.println(entry.getFilename())之后再次进入; ,我得到以下ArrayIndexOutOfBoundsException:

4:

at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1747)

at de.postcon.SftpFileHandler.readFiles(SftpFileHandler.java:129)

at de.postcon.VaFtp2EmMover.downloadVAFiles(VaFtp2EmMover.java:145)

at de.postcon.VaFtp2EmMover.main(VaFtp2EmMover.java:84)

Caused by: java.lang.ArrayIndexOutOfBoundsException

at java.lang.System.arraycopy(Native Method)

at com.jcraft.jsch.Buffer.getByte(Buffer.java:148)

at com.jcraft.jsch.Buffer.getString(Buffer.java:188)

at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1675)

... 3 more

解:

public Map readFiles () {

final Map vaFiles = new HashMap();

final Channel channel;

try {

channel = session.openChannel("sftp");

channel.connect();

final List ftpAccounts = Arrays.stream(this.ftpAccounts).collect(Collectors.toList());

// final List ftpFiles = new ArrayList<>();

for (final String ftpAccount : ftpAccounts) {

// System.out.println(ftpAccount);

try {

try {

final SftpATTRS attrs = sftp.stat(ftpAccount + File.separator + XXX_DIR);

} catch (Exception e) {

// The folder ftpAccount + File.separator + XXX_DIR does not exist.

// Simply continue...

continue;

}

final Vector files = sftp.ls(ftpAccount + File.separator + "*");

final List ftpFileList = files.stream().collect(Collectors.toList());

final List entries = new ArrayList();

for (final ChannelSftp.LsEntry lsEntry : ftpFileList) {

if (lsEntry.getAttrs().isDir() &&

lsEntry.getFilename().equals(XXX_DIR)) {

sftp.ls(ftpAccount + File.separator + lsEntry.getFilename() +

File.separator + "*", new ChannelSftp.LsEntrySelector() {

@Override

public int select (final ChannelSftp.LsEntry entry) {

final Matcher mtc = pattern.matcher(entry.getFilename());

final SftpATTRS attr = entry.getAttrs();

if (mtc.find() && !attr.isDir() && !attr.isLink()) {

// gather the files to be read...

entries.add(entry);

}

return CONTINUE;

}

});

}//end if

}//end for lsEntry

// store the gathered files...

for( final ChannelSftp.LsEntry entry : entries) {

// System.out.println(entry.getFilename());

try {

final ByteArrayOutputStream baos = new ByteArrayOutputStream();

sftp.get(ftpAccount + File.separator + XXX_DIR + File.separator +

entry.getFilename(), baos);

vaFiles.put(entry.getFilename(), baos.toByteArray());

logger.info("File " + ftpAccount + File.separator + XXX_DIR + File.separator + entry.getFilename() + " downloaded.");

baos.close();

} catch (IOException e) {

e.printStackTrace();

} catch (SftpException e) {

e.printStackTrace();

}

}//end for

} catch (SftpException e) {

e.printStackTrace();

}

break;

}

} catch (JSchException e) {

e.printStackTrace();

}

return vaFiles;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值