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