如何检查FTP中存在的文件

时间:2018-04-17 01:58:44

标签: java grails groovy filemaker

大家好,

我试图测试输入文件是否存在于FTP服务器中。如果文件存在,我想显示文件存在。如果没有,则显示文件未找到消息。

see the below code once 

import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPFile;
import java.text.SimpleDateFormat;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat
import groovy.io.FileType

  start()
  def start(){
  def ftpClient = new FTPClient()
  ftpClient.connect(server)                
  ftpClient.login(user,pass)
  ftpClient.enterLocalPassiveMode()
  FTPFile[]  f= ftpClient.listFiles("/Autoplus.txt");
  for(FTPFile ff : f){

      if(ff.exists()){
        return "File existed"
      }
        else{
         return "File not found!"
      }
   }
}

如果有任何错误,请执行上述操作

由于

1 个答案:

答案 0 :(得分:0)

def filePath = "/tmp/file.json" 
def file = new File(filePath) 
assert file.exists() : "file found"