-1

I´m trying to download a file with a local Android project by FTP
This is my method

@Override protected File doInBackground(Void... params) {

    File fileBar = new File(mLocalDir, mFileNameBar);

    boolean descargado = false;
    FTPClient ftpClient = null;
    try {
        ftpClient = new FTPClient();
        ftpClient.setConnectTimeout(10000);
        ftpClient.connect(SERVER, 21);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        if (ftpClient.login(mUser, mPass)) {

            ftpClient.changeWorkingDirectory("/Almacen");

            FileOutputStream fileOutputStreamBar;
            fileOutputStreamBar = new FileOutputStream(fileBar, false);
            OutputStream outputStreamBar = new BufferedOutputStream(fileOutputStreamBar);

            FileOutputStream fileOutputStreamPec;
            fileOutputStreamPec = new FileOutputStream(filePec, false);
            OutputStream outputStreamPec = new BufferedOutputStream(fileOutputStreamPec);


            descargado = ftpClient.retrieveFile(mFileNameBar, outputStreamBar);
        }
    } catch (IOException e) {
        System.out.println(e.toString());
        fileArt = null;

    } finally {
        if (ftpClient != null && ftpClient.isConnected()) try {
            ftpClient.logout();
        } catch (IOException ignored) {
        }
        if (ftpClient != null) try {
            ftpClient.disconnect();
        } catch (IOException ignored) {
        }
    }
    return (descargado ? fileArt : null);

}

The method works correctly, or at least I think so. It download the requested file, but instead of occupying 32Kb it occupies 24Kb. I do not understand why this happens. If anyone can give me a hand I will be very grateful

ZamoraFTW
  • 47
  • 1
  • 7

1 Answers1

2

Make sure to close the output streams gracefully before the function returns. you can try to explicitly call flush() or close(). It seems like your method return before saving all the data.

See this answer

ApriOri
  • 2,618
  • 29
  • 48