简体   繁体   中英

FTP server dies while ftp4j client downloads a file

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM