Multi-threaded querying for files on FTP server

Source: Internet
Author: User

This is the case with a recent automated project that, when batch runs successfully, there will be a specific build file on the FTP server. But I'm not sure when it's going to be, it's about 5 minutes of batch running, so when you set up checkpoints in the script, you need to refresh the files on the FTP server at some time.

On the net down a serv-u to build a local FTP Server, it is easy to build debugging. And then a COMMONS-NET-3.3.JAR packet to deal with the FTP-related business.

You may not need to use multi-threaded, but try the next, single-threaded to exit and then log back on to the FTP server,ftp Server files are not automatically refreshed. So in the main function, I start a thread every once in a while to see if the file I'm looking for exists.

Importjava.io.IOException;Importorg.apache.commons.net.ftp.FTPClient;ImportOrg.apache.commons.net.ftp.FTPFile;Importorg.apache.commons.net.ftp.FTPReply; Public classFtputilextendsThread {PrivateFtpClient ftpclient =Newftpclient (); PrivateString ftpdict = "."; Private BooleanFlag; Private Static BooleanFind =false; Private Static Longtimeslot = 10000;  Public BooleanConnect (String Host,intport, string Username, string password)throwsIOException {ftpclient.connect (host, Port); if(Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {if(ftpclient.login (username, password)) {return true;        }} ftpclient.disconnect (); return false; }     Public Booleanfileexist (String dict, String filename) {string Pattren= "\\. {1,} "; Ftpdict= Ftpdict + "/" +dict; Try{ftpfile[] files=ftpclient.listfiles (ftpdict);  for(inti = 0; i < files.length; i++) {                if(Files[i].getname (). Equalsignorecase (filename)) {flag=true; }                if(Files[i].isdirectory ()&&!Files[i].getname (). Matches (Pattren))                {FileExist (Files[i].getname (), filename); }            }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnFlag; }     Public voidDisconnect ()throwsIOException {if(ftpclient.isconnected ()) {ftpclient.disconnect (); }    }     Public voidrun () {System.out.println (GetName ()+ "Thread started!"); Ftputil FTP=NewFtputil (); Try{ftp.connect ("127.0.0.1", +, "admin", "admin"); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        Booleanb = Ftp.fileexist (ftp.ftpdict, "3.txt");        System.err.println (b); if(b) {Find=true; }        Try{ftp.disconnect (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println (GetName ()+ "Thread ended!"); } @SuppressWarnings ("Static-access")     Public Static voidMain (string[] args)throwsException {//TODO auto-generated Method Stub        intindex = 0;  while(!find) {            NewFtputil (). Start ();            Thread.CurrentThread (). Sleep (timeslot); if(Index = = 4) {                return; } Index++; }    }}

I think there should be an API is to refresh the FTP server, and has not yet groped, have to tell a thank you.

Multi-threaded querying for files on FTP server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.