Packagecom.rick.utils;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportOrg.apache.commons.net.ftp.FTP;Importorg.apache.commons.net.ftp.FTPClient;ImportOrg.apache.commons.net.ftp.FTPFile;Importorg.apache.commons.net.ftp.FTPReply;/***-------------------------------------------* title:ftputil * description:ftp upload Download Tool class * Create on:2017 on April 4 Afternoon 11:14:09 * Copyright (C) strongunion *@authorRICK * Revision history: * Modified Person Modification Date Description *-------------------------------------------*/ Public classFtputil {/*** Description: Uploading files to FTP server *@paramHost FTP server hostname *@paramPort FTP Server ports *@paramUsername FTP login Account *@paramPassword FTP login password *@parambasepath FTP Server base directory *@paramFilePath FTP Server file storage path. For example, the date of storage:/2015/01/01. The path to the file is Basepath+filepath *@paramfilename uploaded to the FTP server *@paramInput Stream *@returnSuccess returns True, otherwise false*/ Public Static BooleanUploadFile (String host,intport, string Username, string password, string basepath, String filePath, string filename, InputStream INP UT) {Booleanresult =false; FtpClient FTP=Newftpclient (); Try { intreply; Ftp.connect (host, port);//connecting to an FTP server//If you use the default port, you can connect directly to the FTP server using Ftp.connect (host)Ftp.login (username, password);//LoginReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); returnresult; } //switch to upload directory if(!ftp.changeworkingdirectory (basepath+FilePath)) { //Create directory If directory does not existstring[] dirs = Filepath.split ("/"); String TempPath=BasePath; for(String dir:dirs) {if(NULL= = Dir | | "". Equals (dir))Continue; TempPath+= "/" +dir; if(!ftp.changeworkingdirectory (TempPath)) { if(!ftp.makedirectory (TempPath)) { returnresult; } Else{ftp.changeworkingdirectory (TempPath); } } } } //set the type of upload file to binary typeFtp.setfiletype (Ftp.binary_file_type); //Uploading Files if(!ftp.storefile (filename, input)) { returnresult; } input.close (); Ftp.logout (); Result=true; } Catch(IOException e) {e.printstacktrace (); } finally { if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException IoE) {}}} returnresult; } /*** Description: Download files from FTP server *@paramHost FTP server hostname *@paramPort FTP Server ports *@paramUsername FTP login Account *@paramPassword FTP login password *@paramRemotePath The relative path on the FTP server *@paramfilename to download *@paramLocalPath saved to local path after download *@return */ Public Static BooleanDownloadFile (String host,intport, string Username, string password, string remotepath, String fileName, String localPath) { Booleanresult =false; FtpClient FTP=Newftpclient (); Try { intreply; Ftp.connect (host, Port); //If you use the default port, you can connect directly to the FTP server using Ftp.connect (host)Ftp.login (username, password);//LoginReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply)) {Ftp.disconnect (); returnresult; } ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directoryftpfile[] fs =Ftp.listfiles (); for(Ftpfile ff:fs) {if(Ff.getname (). Equals (FileName)) {File LocalFile=NewFile (LocalPath + "/" +ff.getname ()); OutputStream is=NewFileOutputStream (LocalFile); Ftp.retrievefile (Ff.getname (), is); Is.close (); }} ftp.logout (); Result=true; } Catch(IOException e) {e.printstacktrace (); } finally { if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException IoE) {}}} returnresult; } Public Static voidMain (string[] args) {Try{FileInputStream in=NewFileInputStream (NewFile ("C:\\users\\rick\\pictures\\saved pictures\\fendou.jpg")); BooleanFlag = Ftputil.uploadfile ("192.168.2.37", +, "Ftpuser", "Ftpuser", "/home/ftpuser/images", "/2017/01/16", "hello.jpg", in); SYSTEM.OUT.PRINTLN (flag); } Catch(FileNotFoundException e) {e.printstacktrace (); } }}
170404, Java version of the FTP Operation tool class