2015-08-29 2 views
0

Это мой код:Как загрузить каталог на FTP?

public void UploadIt(){ 
    org.apache.commons.net.ftp.FTPClient con = null; 

    try 
    { 
     con = new FTPClient(); 
     con.connect("ftp server"); 

     if (con.login("username", "pass")) 
     { 
      con.enterLocalPassiveMode(); // important! 
      con.setFileType(FTP.BINARY_FILE_TYPE); 
      String data = baseDir + "/emre"; 

      FileInputStream in = new FileInputStream(new File(data)); 
      boolean result = con.storeFile("/", in); 
      in.close(); 
      if (result) Log.v("upload result", "succeeded"); 
      con.logout(); 
      con.disconnect(); 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     Log.v(" dead","ddsd"); 
    } 

} 

Я могу загрузить файл, но я не могу загрузить directory.When Я пытаюсь загрузить реж или папку, он говорит: «... Является ли директория», и это не загружает.

ответ

0

Вы должны загружать файлы рекурсивно из каталога после создания каталога на ftp, так как создание папки и создание файлов на ftp не может быть выполнено одновременно, это отдельные команды.

public static void uploadDirectory(FTPClient ftpClient, 
     String remoteDirPath, String localParentDir, String remoteParentDir) 
     throws IOException { 

    System.out.println("LISTING directory: " + localParentDir); 

    File localDir = new File(localParentDir); 
    File[] subFiles = localDir.listFiles(); 
    if (subFiles != null && subFiles.length > 0) { 
     for (File item : subFiles) { 
      String remoteFilePath = remoteDirPath + "/" + remoteParentDir 
        + "/" + item.getName(); 
      if (remoteParentDir.equals("")) { 
       remoteFilePath = remoteDirPath + "/" + item.getName(); 
      } 


      if (item.isFile()) { 
       // upload the file 
       String localFilePath = item.getAbsolutePath(); 
       System.out.println("About to upload the file: " + localFilePath); 
       boolean uploaded = uploadSingleFile(ftpClient, 
         localFilePath, remoteFilePath); 
       if (uploaded) { 
        System.out.println("UPLOADED a file to: " 
          + remoteFilePath); 
       } else { 
        System.out.println("COULD NOT upload the file: " 
          + localFilePath); 
       } 
      } else { 
       // create directory on the server 
       boolean created = ftpClient.makeDirectory(remoteFilePath); 
       if (created) { 
        System.out.println("CREATED the directory: " 
          + remoteFilePath); 
       } else { 
        System.out.println("COULD NOT create the directory: " 
          + remoteFilePath); 
       } 

       // upload the sub directory 
       String parent = remoteParentDir + "/" + item.getName(); 
       if (remoteParentDir.equals("")) { 
        parent = item.getName(); 
       } 

       localParentDir = item.getAbsolutePath(); 
       uploadDirectory(ftpClient, remoteDirPath, localParentDir, 
         parent); 
      } 
     } 
    } 
} 
public static boolean uploadSingleFile(FTPClient ftpClient, 
     String localFilePath, String remoteFilePath) throws IOException { 
    File localFile = new File(localFilePath); 

    InputStream inputStream = new FileInputStream(localFile); 
    try { 
     ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
     return ftpClient.storeFile(remoteFilePath, inputStream); 
    } finally { 
     inputStream.close(); 
    } 
} 

Источник: http://www.codejava.net/java-se/networking/ftp/how-to-upload-a-directory-to-a-ftp-server

+0

Как я могу это сделать в коде? – emre3461

+0

@ emre3461 .. Если вы расскажете о Google, вы получите много ответов на это. Я обновил ответ от одного. – Garry

Смежные вопросы