2012-11-01 101 views
4

[已解決]如何獲取我的FTP服務器上的文件和文件夾列表?如何使用android列出ftp目錄?

我知道如何連接和上傳文件,但不知道如何獲得目錄列表:

  try { 
       FTPClient ftpClient = new FTPClient(); 
       ftpClient.connect(InetAddress.getByName("176.28.25.46")); 
       ftpClient.login("******", "******"); 
       System.out.println("status :: " + ftpClient.getStatus()); 

       ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
       ftpClient.enterLocalPassiveMode(); 

       // Prepare file to be uploaded to FTP Server 
       File file = new File("default.prop"); 
       FileInputStream ifile = new FileInputStream(file); 

       // Upload file to FTP Server 
       ftpClient.storeFile("/subdomains/giveyourapps/httpdocs/apps/default.prop",ifile); 
       ftpClient.disconnect(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

但每次剪斷的代碼,我在谷歌找到了我沒有工作: -/

  try { 

       FTPClient ftpClient = new FTPClient(); 
       ftpClient.connect(InetAddress.getByName("176.28.25.46")); 
       ftpClient.login("******", "******"); 
       System.out.println("status :: " + ftpClient.getStatus()); 

       String toppath = new String(); 
       FTPFile[] ftpDirs = ftpClient.listDirectories(); 
       for (int i = 0; i < ftpDirs.length; i++) { 
        toppath = ftpDirs[0].getName(); 
        Log.d("CONNECT", "Directories in the ftp server are " 
          + ftpDirs[i].getName()); 
       } 

       FTPFile[] ftpdirs2 = ftpClient.listFiles(toppath); 
       for (int i = 0; i < ftpdirs2.length; i++) { 
        Log.d("CONNECT", 
          "File i need is " + ftpdirs2[i].getName()); 
       } 

      } 

對於每個人都有同樣的問題。現在它與代碼:(感謝user1106888)

  try { 

       FTPClient ftpClient = new FTPClient(); 
       ftpClient.connect(InetAddress.getByName("176.28.25.46")); 
       ftpClient.login("******", "******"); 
       System.out.println("status :: " + ftpClient.getStatus()); 
       ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
       ftpClient.enterLocalPassiveMode(); 

       try{ 
       String toppath = new String(); 
       FTPFile[] ftpDirs = ftpClient.listDirectories(); 
       for (int i = 0; i < ftpDirs.length; i++) { 
        toppath = ftpDirs[0].getName(); 
        System.out.println("Directory->: " + ftpDirs[i].getName()); 

       } 

       FTPFile[] ftpdirs2 = ftpClient.listFiles(toppath); 
       for (int i = 0; i < ftpdirs2.length; i++) { 
        System.out.println("Files->: " + ftpdirs2[i].getName()); 
       } 
       }catch (Exception e) { 
        e.printStackTrace(); 
       } 


      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

回答

1

使用此代碼,它應該幫助

FTPFile[] ftpDirs = mFTPClient.listDirectories(); 
        for (int i = 0; i < ftpDirs.length; i++) { 
         toppath = ftpDirs[0].getName(); 
         Log.d("CONNECT", "Directories in the ftp server are " 
           + ftpDirs[i].getName()); 
        } 

        FTPFile[] ftpdirs2 = mFTPClient.listFiles(toppath); 
        for (int i = 0; i < ftpdirs2.length; i++) { 
         Log.d("CONNECT", 
           "File i need is " + ftpdirs2[i].getName()); 
        } 
+0

嗨。它給了我以下錯誤: 'org.apache.commons.net.ftp.FTPConnectionClosedException:連接關閉,無指示。' –

+0

好吧,我想出了爲什麼。我需要setFileType爲FTP.BINARY_FILE_TYPE和enterLocalPassivMode。 –

1

您可以使用CkFtp2 API輕鬆地獲得FTP目錄列表信息。像下面這樣:

CkFtp2 ftp = new CkFtp2(); 

int n = ftp.get_NumFilesAndDirs(); 
    if (n < 0) { 
     outStr += ftp.lastErrorText() + "\n"; 
     tv.setText(outStr); 
     setContentView(tv); 
     return; 
    } 

    if (n > 0) { 
     for (int i = 0; i <= n - 1; i++) { 

      // Display the filename 
      outStr += ftp.getFilename(i) + "\n"; 

      // Display the file size (in bytes) 
      outStr += ftp.GetSize(i) + "\n"; 

      // Is this a sub-directory? 
      if (ftp.GetIsDirectory(i) == true) { 
       outStr += ".. this is a sub-directory" + "\n"; 
      } 

      outStr += "--" + "\n"; 
     } 

    } 
+0

我無法找到與聲明中的工作說明......我將尋找明天.... –

相關問題