javaandroidftpapache-commons-net

Corrupted PDF file downloaded from FTP to a device with Apache Common Net


I´m trying to download a file from a FileZilla Server to an Android device. Everything appears to be rigth, the connection to the FTP, the login, the file exists and the response of the download is OK. But when i go to the file in the Android device it only has a size of 64KB, It must be and I can´t open due to is corrupted... I had tryied whith another file of 49KB and download 48KB and it´s corrupted too. I´m using Adobe Acrobat Reader and QuickOffice to open the files.

String usuarioLogin = params[0];

FTPClient ftp = null;
String LOG_TAG = "FTP";
ftp = new FTPClient();
String server = params[0];
int portNumber = Integer.parseInt(params[1]);
String user = params[2];
String password = params[3];
String filename = params[4];
File localFile = UtilsVictor.crearFichero(params[5]);

ftp.connect(server, portNumber);
Log.d(LOG_TAG, "Connected. Reply: " + ftp.getReplyString());
ftp.enterLocalPassiveMode();

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

OutputStream outputStream = null;
boolean success = false;
outputStream = new BufferedOutputStream(new FileOutputStream(
        localFile));
success = ftp.retrieveFile(filename, outputStream);

if(success) {
    Log.d(LOG_TAG, "success!!");
} else{
    Log.d(LOG_TAG, "NOOOOT success!!");
}

The log of the FileZilla server:

(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> Connected on port 50000, sending welcome message...
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220-FileZilla Server 0.9.60 beta
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 220 Please visit https://filezilla-project.org/
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> USER VICTOR
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> 331 Password required for victor
(000019)13/01/2018 17:13:14 - (not logged in) (192.168.1.38)> PASS ****
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 230 Logged on
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> TYPE I
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 200 Type set to I
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> PASV
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 227 Entering Passive Mode (192,168,1,41,125,49)
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> RETR /Informes/572760344M/aaa.pdf
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 150 Opening data channel for file download from server of "/Informes/572760344M/aaa.pdf"
(000019)13/01/2018 17:13:14 - victor (192.168.1.38)> 226 Successfully transferred "/Informes/572760344M/aaa.pdf"
(000019)13/01/2018 17:13:48 - victor (192.168.1.38)> disconnected.

Where is the problem??


Solution

  • You are missing

    outputStream.close();
    

    No need to rewrite your code completely. Your new code just unnecessarily reimplements, what FTPClient.retrieveFile does internally.

    And even if you choose to use FTPClient.retrieveFileStream, use Util.copyStream) instead of implementing the copy loop yourself.