Java 从FTP下载时如何检查文件是否存在

Java 从FTP下载时如何检查文件是否存在,java,ftp,download,file-exists,Java,Ftp,Download,File Exists,我正在从FTP服务器下载,我不知道如何确切地检查文件是否已经存在。我要做的是从FTP服务器检索filname,然后将其与文件夹中的所有文件进行比较。若文件已经存在,那个么它会将下一个FTP文件名与文件夹中的所有文件进行比较,以此类推。 我已经做了比较,若文件夹中的所有文件都和FTP服务器上的文件同名,那个么它就工作了,但若我添加了一些旧文件,那个么它会再次下载所有文件,我不希望这样 这是我的草稿代码: String[] names = client.listNames(); Fi

我正在从FTP服务器下载,我不知道如何确切地检查文件是否已经存在。我要做的是从FTP服务器检索filname,然后将其与文件夹中的所有文件进行比较。若文件已经存在,那个么它会将下一个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
String[]name=client.listNames();
文件夹=新文件(“c:\\test\\RTR\u ZIP\”;
字符串[]文件名=文件夹.list();

对于(;i您应该使用
java.io.File.exists
java.io.File.isFile()|isDirectory()
检查是否存在,如果您的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();
 }
}

也许它对有同样问题的人有用。我用这种方法编制了程序:

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

  }
}
PackageJavaApplication2;
导入java.io.BufferedInputStream;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入org.apache.commons.net.ftp.*;
公共类DLFile{
public static void saveZIP()引发异常{
FTPClient client=新的FTPClient();
FileOutputStream new_file=null;
字符串server=“server”;
字符串user=“user”;
字符串pass=“pass”;
字符串名称=”;
String downloadFolder=“下载文件夹”;
布尔存在=空;
int i=0;
int j=0;
client.connect(服务器);
client.login(用户,通行证);
client.changeWorkingDirectory(“/rtr/”);
//读取ftp内容
字符串[]名称=client.listNames();
文件夹=新文件(下载文件夹);
字符串[]文件名=文件夹.list();

对于(;i@Igor
java.io.File.equals
。我不明白为什么需要比较文件。正如我所说的,如果目录中有任何较旧的文件,那么我的比较就不起作用。它只在FTP中的文件与目录中的文件相同时起作用。这就是为什么我要将FTP中的文件与目录中的每个文件进行比较