ftp using Groovy and Grape UPDATE

People always look at me in disbelieve when I say I use groovy to setup a bunch of cron scripts to ftp and process millions of records daily. Then they are even more surprised when I said I can set it up in a matter of minutes. Here is how I do it.

import org.apache.commons.net.ftp.FTPClient

start()

@Grab(group='commons-net', module='commons-net', version='2.0')
def start()
{
  def ftpClient = new FTPClient()
  ftpClient.connect("ftp server address here")
  ftpClient.enterLocalPassiveMode()
  println(ftpClient.replyString)
  ftpClient.login("username","password")
  println(ftpClient.replyString)
  ftpClient.changeWorkingDirectory("directory the file is in")
  println(ftpClient.replyString)
  ftpClient.fileType=(FTPClient.BINARY_FILE_TYPE)
  println(ftpClient.replyString)

  def incomingFile = new File("localfilename")
  incomingFile.withOutputStream { ostream ->
    ftpClient.retrieveFile("remotefilename", ostream )
  }
  println(ftpClient.replyString);
  ftpClient.disconnect()
}

I use Grab so I don’t have to worry about dependencies. At each step after issuing a command, I then do a “println(ftpClient.replyString)” to see the output for diagnostic purposes. I’m also using “enterLocalPassiveMode()” to use ftp passive mode. I wrapped it in start() to work around the @Grab in Groovy 1.6 must be attached to something. You won’t need to do that if you are using Groovy 1.7. I also set the transfer mode to explicitly binary because most of the times I’m dealing with zip files.

Then I open a local file, use Groovy’s with block to manage the download. Then disconnect.

Groovy makes this so simple it takes me longer to explain it than writing the code, which is why I post it here instead of keep emailing this to everyone who asked. :)

UPDATE: Guillaume Laforge, the master of Groovy himself suggested I use with.{} block to avoid repeating the prefix over and over again. He posted it here.

It’s a great suggestion. I must admit to this date, I often forget about with.{}. I’ve reproduced his code below.

@Grab(group='commons-net', module='commons-net', version='2.0')
import org.apache.commons.net.ftp.FTPClient

new FTPClient().with {
    connect "ftp server address here"
    enterLocalPassiveMode()
    login "username", "password"
    changeWorkingDirectory "directory the file is in"
    fileType = FTPClient.BINARY_FILE_TYPE
    def incomingFile = new File("localfilename")
    incomingFile.withOutputStream { ostream -> retrieveFile "remotefilename", ostream }
    disconnect()
}

Comments (6)

Lucas TeixeiraNovember 16th, 2009 at 7:35 pm

Great!!!

Grape is perfect for scripts and routines! :)

MNovember 16th, 2009 at 7:39 pm

That it is. One of my favorite features in 1.6.

John RellisNovember 17th, 2009 at 6:05 am

now that is nice!

[...] ftp using Groovy and Grape groovy!! [...]

DANNYDecember 3rd, 2009 at 7:28 am

How to delete the remote File , hope your help , thanks~~~

MDecember 3rd, 2009 at 11:34 am

you can use ftpClient.deleteFile(pathname)