【问题标题】:How can I check if a file exists on FTP server?如何检查 FTP 服务器上是否存在文件?
【发布时间】:2011-11-16 22:33:30
【问题描述】:

我在 android 上使用 apache FTPClient。我想从 ftp 服务器下载一个文件。但我想在下载之前检查它是否存在于服务器上。我该如何检查?

谢谢,

我的代码:

public static boolean getFile(String serverName, String userName,
        String password, String serverFilePath, String localFilePath)
        throws Exception {

    FTPClient ftp = new FTPClient();
    try {
        ftp.connect(serverName);
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return false;
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                throw e;
            }
        }
        throw e;
    } catch (Exception e) {
        throw e;
    }

    try {
        if (!ftp.login(userName, password)) {
            ftp.logout();
        }           
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();

        OutputStream output;

        output = new FileOutputStream(localFilePath);           
        ftp.retrieveFile(serverFilePath, output);
        output.close();

        ftp.noop(); // check that control connection is working OK
        ftp.logout();
        return true;

    } catch (FTPConnectionClosedException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw e;
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                throw f;
            }
        }

    }

}

【问题讨论】:

    标签: android file-exists ftp-client


    【解决方案1】:
    String[] files = ftp.listnames();
    

    如果需要的文件名是包含,请查看文件...

    【讨论】:

      【解决方案2】:

      假设ftpClientorg.apache.commons.net.ftp.FTPClient 的一个实例:

      public boolean fileExists(String fileName) throws IOException
      {
          String[] files = ftpClient.listNames();
      
          return Arrays.asList(files).contains(fileName);
      }
      

      【讨论】:

        【解决方案3】:

        当客户端发送 RETR 并且服务器以错误代码 550 响应时,您可以非常确定该文件不存在或者您没有获取它的权限...由于 FTP 规范有点松散,您可能只是假设 550 - 559 范围内的任何错误都表示永久文件系统错误。

        【讨论】:

          【解决方案4】:
          InputStream inputStream = ftpClient.retrieveFileStream(filePath);
           if (inputStream == null || ftpClient.getReplyCode() == 550) {
          // it means that file doesn't exist.
          }
          
          
          or
          
          FTPFile[] mFileArray = ftp.listFiles();
          // you can check if array contains needed file
          

          【讨论】:

          • 必须添加 completePendingCommand() 以防止以下 FTP 命令出现问题。
          猜你喜欢
          • 2012-05-15
          • 1970-01-01
          • 2016-01-13
          • 2018-09-24
          • 1970-01-01
          • 2013-01-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多