Reputation: 16767
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?
Upvotes: 0
Views: 6567
Reputation: 11
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.
Upvotes: 1
Reputation: 2310
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;
}
Upvotes: 0
Reputation: 324
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)?
Upvotes: 1