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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM