Java Tools-Verify that the specified file for the FTP server exists

Source: Internet
Author: User

ImportOrg.apache.commons.net.ftp.FTP;Importorg.apache.commons.net.ftp.FTPClient;Importorg.apache.commons.net.ftp.FTPReply;/*** Verify that the file specified by the path exists in the FTP server *@paramfilepath--Specifies the absolute path of the file *@paramuser--ftp Server login user name *@parampassward--ftp Server login Password *@paramIP--FTP's IP address *@paramPort--ftp's port number *@return*/ Public Static BooleanIsftpfileexist (String filepath,string user,string passward,string IP,intPort) {ftpclient FTP=Newftpclient (); Try{//connecting to an FTP serverftp.connect (IP, port); //Loginftp.login (user, passward); //Verify that the return code for the login operation is correct            if(!ftpreply.ispositivecompletion (Ftp.getreplycode ()))                {Ftp.disconnect (); return false;            } ftp.enterlocalactivemode (); //set file type binary, different from ASCIIFtp.setfiletype (Ftp.binary_file_type); //set the encoding formatFtp.setcontrolencoding ("GBK"); //extract the absolute address of the directory and the file nameFilePath = Filepath.replace ("ftp://" +ip+ ":" +port+ "/", ""); String dir= filepath.substring (0, Filepath.lastindexof ("/")); String file= Filepath.substring (Filepath.lastindexof ("/") +1); //Enter the file directory, note the encoding format, in order to correctly identify the Chinese directoryFtp.changeworkingdirectory (NewString (Dir.getbytes ("GBK") , ftp.default_control_encoding)); //Verify that the file existsInputStream is = Ftp.retrievefilestream (NewString (File.getbytes ("GBK") , ftp.default_control_encoding)); if(IS = =NULL|| Ftp.getreplycode () = =ftpreply.file_unavailable) {                return false; }                        if(Is! =NULL) {is.close ();            Ftp.completependingcommand (); }            return true; } Catch(Exception e) {e.printstacktrace (); }finally{            if(FTP! =NULL){                Try{ftp.disconnect (); } Catch(IOException e) {e.printstacktrace (); }            }        }        return false;}

Java Tools-Verify that the specified file for the FTP server exists

Related Article

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.