1
FileUtils.copyURLToFile(ftpUrl, destFile);

Is there any way I can check if a filename exists in the FileURL on the ftp location?

ftpURL: ftp://username:pwd@ftp.abcd.com/filename

Version used: commons-io-2.1.jar. I looked at commons-io.2.4.jar directoryContains() looks right but its only to check for a file in directory and not for FTP URL.

Thanks.

4

2 回答 2

0

只需捕获异常并进行相应处理

boolean fileCopyWorked =false;
try{

  FileUtils.copyURLToFile(ftpUrl, destFile);
  fileCopyWorked=true;
}
catch(IOException e){
  // indicates there was an error; handle accordingly 
}
于 2013-05-24T20:17:10.477 回答
-1

您可以使用 org.apache.commons.net.ftp.FTPClient

当你可以在你的类中实例化 FTPClient 时,你可以使用以下方法

boolean checkFileExists(String filePath) throws IOException {
        InputStream inputStream = ftpClient.retrieveFileStream(filePath);
        int returnCode = ftpClient.getReplyCode();
        if (inputStream == null || returnCode == 550) {
            return false;
        }
        return true;
    }
于 2013-05-24T19:09:09.260 回答