2010-11-29 1 views
2

FTP 서버에서 다운로드 중이며 파일이 이미 있는지 확인하는 방법을 정확하게 모른다. 내가 원하는 것은 FTP 서버에서 filname을 검색 한 다음 폴더의 모든 파일과 비교하는 것입니다. 파일이 이미 있으면 다음 FTP 파일 이름을 폴더의 모든 파일과 비교합니다. 이미 비교를했는데 폴더의 모든 파일이 FTP 서버의 파일과 동일한 이름을 가지고 있지만 이전 파일을 추가하면 모든 파일을 다시 한 번 다운로드하고 싶지 않습니다. 사전에FTP에서 다운로드 할 때 파일이 존재하는지 확인하는 방법

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 

감사 :

여기 내 스크래치 코드입니다.

답변

2

ftp 서버가 XCRC 명령을 지원하면 로컬 파일과 원격 파일의 체크섬 (CRC32)을 비교할 수 있습니다. 폴더의 모든 파일을 반복하고 해당 crc를 로컬 파일과 비교할 수 있습니다.

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(); 
} 
} 
2

java.io.File.existsjava.io.File.isFile()|isDirectory()을 사용하여 존재 여부를 확인해야합니다.

+0

비교 방법은 어떻게됩니까? – Igor

+0

@Igor'java.io.File.equals'. 왜 파일을 비교해야하는지 이해가 안됩니다. – khachik

+0

내가 말했듯이 디렉토리에 이전 파일이 있으면 내 비교가 작동하지 않습니다. FTP의 파일이 디렉토리의 파일과 동일한 경우에만 작동합니다. 그래서 내가 FTP에서 파일을 디렉토리의 모든 파일과 비교하려고합니다. – Igor

1

아마도 같은 문제가있는 누군가에게 유용 할 것입니다. 이 방법으로 프로그램을 만들었습니다.

package javaapplication2; 

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import org.apache.commons.net.ftp.*; 



public class DLFile { 

    public static void saveZIP() throws Exception { 

     FTPClient client = new FTPClient(); 
     FileOutputStream new_file = null; 
     String server = "server"; 
     String user = "user"; 
     String pass = "pass"; 
     String name = ""; 
     String downloadFolder = "download_folder"; 
     Boolean exists = null; 
     int i=0; 
     int j=0; 

     client.connect(server); 
     client.login(user,pass); 
     client.changeWorkingDirectory("/rtr/"); 

//read ftp content 
      String[] names = client.listNames(); 
      File folder = new File(downloadFolder); 
      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(downloadFolder + name); 
          client.retrieveFile(name, new_file); 
          j++; 
          exists=true; 
         } else { 

//CHECK IF FILE EXISTS        
          if (!new File(downloadFolder + name).exists()) { 
           new_file = new FileOutputStream(downloadFolder + name); 
           client.retrieveFile(name, new_file); 
           j++; 
           exists=true; 
          } 

         }//else 
        }//if contains .zip 
      }//for 

      if (exists = true) { 
       System.out.println("Downloading ZIP files: Downloaded " + j + " files"); 
      } else System.out.println("Downloading ZIP files: Files already exist."); 

      client.logout(); 

    } 
}