0

I'm using Apache Commons FTPClient to fetch files from FTP server. This is the setup:

 ftpClient.setDefaultPort(port);
            ftpClient.connect(server);
            ftpClient.login(user, password);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.changeWorkingDirectory(path);

This is the transfer code:

final FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
                    final boolean result = ftpClient.retrieveFile(dirToList + aFile.getName(), fileOutputStream);
                    Log.i(TAG, "[" + (result ? "+" : "-") + "]");

And what I see in the logs:

I/SyncService( 4412): /Users/user1/Downloads/FtpSync/.idea/copyrightprofiles_settings.xml
I/SyncService( 4412): [-]
<...>
I/SyncService( 4412): /Users/user1/Downloads/FtpSync/footer.php
I/SyncService( 4412): [+]

All php files are synced, and all xml files are failed to sync. The FTP server is on my local notebook (Mac OS X default ftp server, tnftpd 20100324+GSSAPI)

Why it does not work?

artem
  • 16,382
  • 34
  • 113
  • 189

3 Answers3

1

For first, you should always close the output stream after the retrieveFile method. Have you tried to change the FTP.{filetype} when downloading XML files (although this shouldnt be the case)?

awb
  • 324
  • 2
  • 9
  • Changed to ASCII, this didn't help. – artem Aug 03 '13 at 14:37
  • Your code does not seem to be wrong. My next idea would go to user priviledges. Are you sure that your ftp user account has the required rights to access the xml? And did you try checking server response for your download request? – awb Aug 03 '13 at 22:46
  • I'm connecting with rankor777 account, /Users/rankor777 is home of this user. I'll try to check it manually with ftp command. – artem Aug 04 '13 at 19:14
1

I have had trouble downloading some large files with the retrieveFile method, where it would crash without throwing an exception. In the end I used the retrieveFileStream method, which solved it for me.

Replace

status = mFTPClient.retrieveFile(srcFilePath, desFileStream);

With

// import org.apache.commons.io.IOUtils;

InputStream inputStream = mFTPClient.retrieveFileStream(srcFilePath);
IOUtils.copy(inputStream, desFileStream);
outputStream.flush();
IOUtils.closeQuietly(desFileStream);
IOUtils.closeQuietly(inputStream);

//status = mFTPClient.completePendingCommand();
status = true;

completePendingCommand crashed without throwing an exception for me, hence why it is commented out, but I think it is supposed to be called after completing a command.

bixy
  • 11
  • 1
  • 2
0

use this code to download files.

public boolean ftpDownload(String srcFilePath, String desFilePath)
    {
        boolean status = false;
        try {
            FileOutputStream desFileStream = new FileOutputStream(desFilePath);; //desfilepath where the file is to be stored
            status = mFTPClient.retrieveFile(srcFilePath, desFileStream);

            desFileStream.close();

            return status;
        } catch (Exception e) {
            Log.d(TAG, "download failed");
        }

        return status;
    } 
Prakhar
  • 2,270
  • 19
  • 26