2014-07-07 19 views

Odpowiedz

10

Wykorzystanie biblioteki commons.apache.org/proper/commons-net

Sprawdź poniżej kod, aby pobrać plik z serwera FTP:

private Boolean downloadAndSaveFile(String server, int portNumber, 
    String user, String password, String filename, File localFile) 
    throws IOException { 
FTPClient ftp = null; 

try { 
    ftp = new FTPClient(); 
    ftp.connect(server, portNumber); 
    Log.d(LOG_TAG, "Connected. Reply: " + ftp.getReplyString()); 

    ftp.login(user, password); 
    Log.d(LOG_TAG, "Logged in"); 
    ftp.setFileType(FTP.BINARY_FILE_TYPE); 
    Log.d(LOG_TAG, "Downloading"); 
    ftp.enterLocalPassiveMode(); 

    OutputStream outputStream = null; 
    boolean success = false; 
    try { 
     outputStream = new BufferedOutputStream(new FileOutputStream(
       localFile)); 
     success = ftp.retrieveFile(filename, outputStream); 
    } finally { 
     if (outputStream != null) { 
      outputStream.close(); 
     } 
    } 

    return success; 
} finally { 
    if (ftp != null) { 
     ftp.logout(); 
     ftp.disconnect(); 
    } 
} 
} 
+0

Witam, zainstalowałem serwer FTP FileZilla moim pulpicie. Jego adres to 127.0.0.1, port to 14147. Co muszę podać dla nazwy użytkownika, hasła, nazwy pliku, pliku lokalnego? –

+0

Użyj tego linku http://lifehacker.com/339887/build-a-home-ftp-server-with-filezilla, aby utworzyć użytkownika z filezillą i wiedzieć, jak go używać. LUB sprawdź to dla tych samych ustawień http://www.addictivetips.com/windows-tips/how-to-setup-personal-ftp-server-using-filezilla-step-by-step-guide/ –

+0

Co to jest biblioteka używają??? Dostaję java.lang.NullPointerException na org.apache.commons.net.ftp.FTP.sendCommand (FTP.java:471) –

Powiązane problemy