2013-04-21 150 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

相关问题