2013-04-21 84 views
-2

我必須從我的服務器下載.txt格式的文件到我的Android機器。但我不知道如何從服務器下載包含用戶名以及密碼的文件以下載文件。有沒有人幫助我。我是android編程的新手。如何從FTP服務器下載.txt文件

+0

檢查這個線程http://stackoverflow.com/questions/16128559/how-to-download-a-txt-file-from-ftp-server – 2013-04-21 06:30:57

回答

1

尋找公共網絡apache庫 - http://commons.apache.org/proper/commons-net/。它提供了一個使用FTP的工具。

代碼示例

FileOutputStream videoOut; 
    File targetFile = new File("path"); 

    FTPClient ftpClient = new FTPClient(); 
    try { 
     ftpClient.connect("your.ftp.address", 21); 
     ftpClient.enterLocalPassiveMode(); 
     ftpClient.login("login", "password"); 

     ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);// Used for video 
     targetFile.createNewFile(); 
     videoOut = new FileOutputStream(targetFile); 
     boolean result=ftpClient.retrieveFile("/" + "filename-to-download", videoOut); 

     ftpClient.disconnect(); 
     videoOut.close();  

    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 
+0

請給它。 txt格式 – nitesh 2013-04-21 07:57:36

+7

不要懶惰nitesh – xDragonZ 2013-04-21 08:02:05

+1

您可以使用FTPClient.ASCII_FILE_TYPE。爲了解不同之處,請閱讀http://www.unix.com/answers-frequently-asked-questions/13574-text-files-ascii-files-binary-files-ftp-transfers.html – Viacheslav 2013-04-21 08:22:06

相關問題