ftp不能创建多级目录【循环创建问题】

关于报表导出用上了ftp文件服务器,最开始用字符串存储了文件上传路径,然后使用ftp.makedir(path),后来发现好像不能用它自带的方法创建多级目录。然后上网查,根据大佬们的博客得到了适用的多级目录创建方法。在此可以记录一下。

/**
* ftp创建目录——ftpClient只支持一级一级创建
*
* @param ftp
* @param path
* @return
* @throws IOException
*/
boolean makeDir(FTPClient ftp, String path) throws IOException {
// 分割
String[] paths = path.split("/");
// 创建成功标识
boolean isMakeSucess = false;
// 遍历每一级路径
for (String str : paths) {
// 切换目录,根据切换是否成功判断子目录是否存在
boolean changeSuccess = ftp.changeWorkingDirectory(str);
// 该级路径不存在就创建并切换
if (!changeSuccess) {
isMakeSucess = ftp.makeDirectory(str);
ftp.changeWorkingDirectory(str);
}
}
return isMakeSucess;
}

原文地址:https://www.cnblogs.com/Rainbow-sea/p/14309457.html