2:ftp创建多层目录文件夹

问题:在ftp创建文件夹时只能单目录创建,比如"/user";不能一次创建多个"/user/zx",不能满足功能需求

解决方案

 1 /**
 2  * 验证是否存在文件夹 不存在 创建  ISO防止中文乱码
 3  * @param ftpClient
 4  * @param ndir 基本路径
 5  * @param idir 校验路径
 6  * @throws Exception
 7  */
 8 public static void checkDirLoop(FTPClient ftpClient, String ndir ,String idir)
 9       throws Exception {
10    try {
11       boolean flag = false;//不存在
12       String[] dirs = idir.split("\/");
13       String path ="";
14       String charest = ftpClient.getControlEncoding();
15       for(String name : dirs){
16          if(Tools.processNull(name).equals(""))
17             continue;
18          FTPFile[] ftpFileArr = ftpClient.listFiles(new String((ndir+path).getBytes(),charest));
19          path+="/"+name;
20          for (FTPFile ftpFile : ftpFileArr) {//遍历dir是否存在subDir
21             if(ftpFile.isDirectory() && ftpFile.getName().equalsIgnoreCase(new String(name.getBytes(),charest))){
22                flag=true;
23             }
24          }
25          if (!flag) {
26             if (!ftpClient.makeDirectory(new String((ndir+path+"/").getBytes(),charest))) {
27                throw new Exception(ndir+path + "目录创建失败!");
28             }
29          }
30       }
31    } catch (Exception e) {
32       throw new Exception(ndir + idir + "创建失败!");
33    }
34 }

原文地址:https://www.cnblogs.com/karlz/p/14355787.html