Reputation: 775
I use sauronsoftware.ftp4j.FTPClient to do scheduled file downloads from FTP servers. My problem is that FTP server suddenly dies while the client downloads a file from it. This is what i do:
for (FTPFile remoteFile : remoteFiles) {
String remoteFileName = remoteFile.getName();
String localPath = ftpDir.getLocalPath() + remoteFileName;
log.debug("Downloading remote file {} to local path {}", remoteFileName, localPath);
try {
client.download(remoteFileName, new File(localPath));
if (!ftpDir.isLeaveFilesOnServer()) {
//Delete remote file
client.deleteFile(remoteFileName);
}
} catch (IllegalStateException e) {
log.error("FTPException ",e);
fcr.addErrorFile(remoteFileName);
} catch (IOException e) {
log.error("FTPException ",e);
The problem is that download(...) runs by separate thread and when FTP server dies this thread continues to run anyway like forever. Is there a way to come around this problem or should i use another FTP client that can handle cases like this?
Upvotes: 1
Views: 802
Reputation: 10342
I'm not sure if your problem is your FTP connection dies sudden and unexpedtecly, or if the problem is the main thread finished its execution before files are downloaded. If we are talking about the second scenario, then maybe you can use this other method of the same FTPClient class:
public void download(java.lang.String remoteFileName,
java.io.File localFile,
FTPDataTransferListener listener)
and then make the main thread to wait until all downloads have finished before ending
Upvotes: 1