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

发布于 2024-10-04 22:00:09 字数 1560 浏览 5 评论 0原文

我正在从 FTP 服务器下载,但我不知道如何检查文件是否已存在。我想要做的是从 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

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埋葬我深情 2024-10-11 22:00:10

如果您的 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();
 }
}

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();
 }
}
森末i 2024-10-11 22:00:10

您应该使用 java.io.File.exists 和 java.io.File.isFile()|isDirectory() 检查是否存在。

You should check for existence using java.io.File.exists and java.io.File.isFile()|isDirectory().

反话 2024-10-11 22:00:10

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

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

  }
}

Maybe it will be useful to somebody with same problem. I made program by this method:

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

  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文