Search code examples
javaftpdownloadfile-exists

How to check if file exist when downloading from FTP


I'm downloading from FTP server and I don't know exactly how to check if file already exist. What I want to do is that I retrieve filname from FTP server and then compare it with all files in folder. If file already exists then it compares next FTP filename with all files in folder and so on. I already did comparison and it's working if all files from folder have same name as files on FTP server but if I add some older file then it downloads all files once again and I don't want that.

Here is my scratch code:

String[] names = client.listNames();
        File folder = new File("c:\\test\\RTR_ZIP\\");
        String[] filename = folder.list();

        for (;i<names.length;i++) {
            name = names[i];

            exists=false;

                if (name.contains(".zip")) {

                    if (filename.length == 0) {
                        new_file = new FileOutputStream("C:\\test\\RTR_ZIP\\" + name);
                        client.retrieveFile(name, new_file);
                        j++;
                        exists=true;
                    } else {
                            for (;k<filename.length;k++) {
                            name = names[i];
                            i++;
                            name1=filename[k];
    //                        CHECK IF FILE EXISTS
                                    if (!name.equals(name1)) {
                                        new_file = new FileOutputStream("C:\\test\\RTR_ZIP\\" + name);
                                        client.retrieveFile(name, new_file);
                                        j++;
                                        exists=true;
                                    } 
                            }
                      }//else
                }//if contains .zip
        }//for

Thanks in advance.


Solution

  • If your ftp server supports XCRC command it could be possible to compare checksum (CRC32) of local and remote file. You could iterate all files in the folder and compare its crc with local one.

    import java.io.File;
    import java.io.IOException;
    import java.net.SocketException;
    import java.util.Scanner;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.net.ftp.FTPClient;
    
    public class DownloadFile {
    
     private FTPClient client = new FTPClient();
    
     public void connect() throws SocketException, IOException {
      client.connect("127.0.0.1");
      client.login("user", "password");
     }
    
     public boolean hasXCRCSupport() throws IOException {
      client.sendCommand("feat");
      String response = client.getReplyString();
      Scanner scanner = new Scanner(response);
      while(scanner.hasNextLine()) {
       String line = scanner.nextLine();
       if(line.contains("XCRC")) {
        return true;
       }
      }
      return false;
     }
    
     public boolean isSameFile() throws IOException {
      if(hasXCRCSupport()) {
       File file = new File("D:/test.txt");
       String localCRC = Integer.toHexString((int) FileUtils.checksumCRC32(file)).toUpperCase();
       client.sendCommand("XCRC /test.txt");
       String response = client.getReplyString().trim();
       System.out.println(response);
       if(response.endsWith(localCRC)) {
        return true;
       }
      }
      return false;
     }
     public void logout() throws IOException {
      client.logout();
     }
    
     public static void main(String[] args) throws SocketException, IOException {
      DownloadFile downloadFile = new DownloadFile();
      downloadFile.connect();
      if(downloadFile.isSameFile()) {
       System.out.println("remote file is same as local");
      }
      downloadFile.logout();
     }
    }