如何使用 Jsch 递归 FTP 目录结构?
How can I recursively FTP a directory structure using Jsch?
我正在尝试遍历目录并以相同的结构上传所有内容和目录。
这是一个示例结构:
Dir1/
....Dir1_1/
....Dir1_2/
........Dir1_2_1/
............file.txt
Dir2/
....file.txt
....file2.txt
Dir3/
Dir4/
index.html
index.css
example.file
我试过以下方法:
private void ftpFiles(File[] files, ChannelSftp channelSftp) throws SftpException, FileNotFoundException {
for (File file : files) {
System.out.println("Uploading: " + file.getName());
if (file.isDirectory()) {
System.out.println(file.getName() + " is a directory");
SftpATTRS attrs;
try {
channelSftp.stat(file.getName());
} catch (Exception e) {
System.out.println(file.getName() + " does not exist. Creating it...");
channelSftp.mkdir(file.getName());
}
channelSftp.cd(file.getName());
this.ftpFiles(file.listFiles(), channelSftp);
} else {
channelSftp.put(new FileInputStream(file), file.getName());
}
}
}
我正在获取一组顶级文件,并递归地逐一向下移动。
The problem is once I hit the first directory and cd
into it, all further files and directories are inside of this.
示例: /Dir1/Dir1_1/Dir1_2/Dir1_2_1/Dir2/Dir3/Dir4/...etc
如何在递归调用时对我的频道执行 ../
?
可能类似于(伪代码):
List<Files> directories = new ArrayList<> ();
if (file is directory) directories.add(file);
else dowloadFile();
for (File f : directories) {
cd(f);
ftpFiles(listFiles());
cd(..);
}
我正在尝试遍历目录并以相同的结构上传所有内容和目录。
这是一个示例结构:
Dir1/
....Dir1_1/
....Dir1_2/
........Dir1_2_1/
............file.txt
Dir2/
....file.txt
....file2.txt
Dir3/
Dir4/
index.html
index.css
example.file
我试过以下方法:
private void ftpFiles(File[] files, ChannelSftp channelSftp) throws SftpException, FileNotFoundException {
for (File file : files) {
System.out.println("Uploading: " + file.getName());
if (file.isDirectory()) {
System.out.println(file.getName() + " is a directory");
SftpATTRS attrs;
try {
channelSftp.stat(file.getName());
} catch (Exception e) {
System.out.println(file.getName() + " does not exist. Creating it...");
channelSftp.mkdir(file.getName());
}
channelSftp.cd(file.getName());
this.ftpFiles(file.listFiles(), channelSftp);
} else {
channelSftp.put(new FileInputStream(file), file.getName());
}
}
}
我正在获取一组顶级文件,并递归地逐一向下移动。
The problem is once I hit the first directory and
cd
into it, all further files and directories are inside of this.
示例: /Dir1/Dir1_1/Dir1_2/Dir1_2_1/Dir2/Dir3/Dir4/...etc
如何在递归调用时对我的频道执行 ../
?
可能类似于(伪代码):
List<Files> directories = new ArrayList<> ();
if (file is directory) directories.add(file);
else dowloadFile();
for (File f : directories) {
cd(f);
ftpFiles(listFiles());
cd(..);
}