-2

Hi Everyone,

I'm trying to test if the input file exist exist in FTP server or not.If The file exist I want to display File exist.If not then display File not found messages.

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!"
      }
   }
}

The above doesn't executed please if any errors in that,please suggest your inputs

Thanks

  • Did you bother to read the javadocs? https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#listFiles(java.lang.String) The parameter is the `path` – Scary Wombat Apr 17 '18 at 02:02
  • do you already test to compile and run that code? any error? – bekt Apr 17 '18 at 02:02
  • Thanks for giving response.yes I have tested but it shows invocation exception occur – Chanbasha Sk Apr 17 '18 at 02:08
  • Duplicate of https://stackoverflow.com/questions/49864175/check-the-file-exist-in-ftp-using-groovy again – tim_yates Apr 17 '18 at 05:16

1 Answers1

0
def filePath = "/tmp/file.json" 
def file = new File(filePath) 
assert file.exists() : "file found"
Sathish vp
  • 72
  • 12