channelsftp的put_在JSch中使用channelsftp传输文件夹和子文件夹?

I want to transfer a folder and a subfolder using channelsftp. I can successfully transfer files using channelsftp.put(src,dest) command but this does not work for folders (at least I could not make it work). So can someone please explain how can I transfer folders and subfolders using channelsftp?

解决方案

To work with multilevel folder structures in jsch you:

enter them;

list their contents;

do smth with every found item;

repeat 1, 2 & 3 if subfolder is found.

DOWNLOAD dirs method inside your JSCH class:

public void downloadDir(String sourcePath, String destPath) throws SftpException { // With subfolders and all files.

// Create local folders if absent.

try {

new File(destPath).mkdirs();

} catch (Exception e) {

System.out.println("Error at : " + destPath);

}

sftpChannel.lcd(destPath);

// Copy remote folders one by one.

lsFolderCopy(sourcePath, destPath); // Separated because loops itself inside for subfolders.

}

private void lsFolderCopy(String sourcePath, String destPath) throws SftpException { // List source (remote, sftp) directory and create a local copy of it - method for every single directory.

Vector list = sftpChannel.ls(sourcePath); // List source directory structure.

for (ChannelSftp.LsEntry oListItem : list) { // Iterate objects in the list to get file/folder names.

if (!oListItem.getAttrs().isDir()) { // If it is a file (not a directory).

if (!(new File(destPath + "/" + oListItem.getFilename())).exists() || (oListItem.getAttrs().getMTime() > Long.valueOf(new File(destPath + "/" + oListItem.getFilename()).lastModified() / (long) 1000).intValue())) { // Download only if changed later.

new File(destPath + "/" + oListItem.getFilename());

sftpChannel.get(sourcePath + "/" + oListItem.getFilename(), destPath + "/" + oListItem.getFilename()); // Grab file from source ([source filename], [destination filename]).

}

} else if (!".".equals(oListItem.getFilename() || "..".equals(oListItem.getFilename())) {

new File(destPath + "/" + oListItem.getFilename()).mkdirs(); // Empty folder copy.

lsFolderCopy(sourcePath + "/" + oListItem.getFilename(), destPath + "/" + oListItem.getFilename()); // Enter found folder on server to read its contents and create locally.

}

}

}

REMOVE dirs method inside your JSCH class:

try {

sftpChannel.cd(dir);

Vector list = sftpChannel.ls(dir); // List source directory structure.

for (ChannelSftp.LsEntry oListItem : list) { // Iterate objects in the list to get file/folder names.

if (!oListItem.getAttrs().isDir()) { // If it is a file (not a directory).

sftpChannel.rm(dir + "/" + oListItem.getFilename()); // Remove file.

} else if (!".".equals(oListItem.getFilename())) { // If it is a subdir.

try {

sftpChannel.rmdir(dir + "/" + oListItem.getFilename()); // Try removing subdir.

} catch (Exception e) { // If subdir is not empty and error occurs.

lsFolderRemove(dir + "/" + oListItem.getFilename()); // Do lsFolderRemove on this subdir to enter it and clear its contents.

}

}

}

sftpChannel.rmdir(dir); // Finally remove the required dir.

} catch (SftpException sftpException) {

System.out.println("Removing " + dir + " failed. It may be already deleted.");

}

CALL these methods from outside like:

MyJSCHClass sftp = new MyJSCHClass();

sftp.removeDir("/mypublic/myfolders");

sftp.disconnect(); // Disconnecting is obligatory - otherwise changes on server can be discarded (e.g. loaded folder disappears).

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值