john renfrew Posted May 23, 2013 Posted May 23, 2013 Just updated my ftp functions to add some functionality for a job. Some of the techniques might prove useful if you want to dig into them Uses the Commons-Net-3.2 jar but should also work with Commons-Net-Ftp-2.0 First also needs Commons-io jar List image files: // FTPlist ( ftp_host ; ftp_user ; ftp_password ; remote_path ) // 13_05_22 JR // v1.0 // returns date sorted list of tab separated file entries // date, time, size, name import org.apache.commons.net.ftp.FTPClient import org.apache.commons.net.ftp.FTPFile import org.apache.commons.io.FileUtils import java.text.SimpleDateFormat try{ new FTPClient().with{ connect ftp_host login ftp_user, ftp_password names = listFiles(remote_path) logout() disconnect() } //end with } catch (e) { return e } //end try sdf = new SimpleDateFormat("dd/MM/yyyytHH:mm") sdg = new SimpleDateFormat("yyyy/MM/dd") sdh = new SimpleDateFormat("HH:mm") list = [] filenames = [] TAB = 't' names.each{ if(it.getType() == FTPFile.FILE_TYPE){ tx = it.getTimestamp().getTime() list.add(new Expando(date:sdg.format(tx), time:sdh.format(tx), fmdate:sdf.format(tx), name:it.getName(), size:FileUtils.byteCountToDisplaySize(it.getSize()))) } //end if } //end each // SORT by date, then time, then name ls = list.sort{x,y -> x.date<=>y.date ?: x.time<=>y.time ?: x.name<=>y.name} ls.each{ filenames += it.fmdate + TAB + it.size + TAB + it.name } //end each return filenames ?: false Get specific file // FTPgetFile ( ftp_host ; ftp_user ; ftp_password ; remote_path ; local_path ; fm_file ) // 13_05_22 JR // v1.0 // downloads file from fTP site, returns true if successful or false or error import org.apache.commons.net.ftp.FTPClient import org.apache.commons.net.ftp.FTP SEP = System.getProperty("file.separator") try{ new FTPClient().with { connect ftp_host enterLocalPassiveMode() login ftp_user, ftp_password changeWorkingDirectory remote_path setFileType(FTP.BINARY_FILE_TYPE) incomingFile = new File(local_path + SEP + fm_file) incomingFile.withOutputStream { ostream -> retrieveFile fm_file, ostream } //end stream logout() disconnect() } //end with } catch (e) { return e } //end try if (!new File(local_path + SEP + fm_file).size()){ return false } return true Delete specific file // FTPdeleteImage ( ftp_host ; ftp_user ; ftp_password ; remote_path ; fm_file ) // 13_05_22 JR // v1.0 // deletes file from fTP site, returns true if successful or false or error import org.apache.commons.net.ftp.FTPClient try{ new FTPClient().with { connect ftp_host enterLocalPassiveMode() login ftp_user, ftp_password changeWorkingDirectory remote_path deleteFile fm_file logout() disconnect() } //end with } catch (e) { return e } //end try return true
Noél Dubau Posted October 31, 2013 Posted October 31, 2013 Hello, I just found that thread and your functions. You call the first one "List image file" ; I tried it and on a directory I got 28/10/2013 16:32 0 bytes bof 28/10/2013 17:01 25 KB 130.gif 30/10/2013 13:52 74 KB Afonso_aline.pdf 30/10/2013 13:52 76 KB Auroire_Christine.pdf 30/10/2013 14:34 6 KB test.txt I thought it listed all file types, but in another directory I had flv files the function returns 0. I'd like to get all files, and even in sub-directories. What must I modify ? Thanks Noël Sorry for the noise : I just find that the path to the remote directory is sensible to the uppercase or lowercase !
Recommended Posts
This topic is 4310 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now