FTP upload download file, is to follow the FTP protocol upload downloaded files, this example only download the file as an example.
Important ways to explain:
1.FTP function dependent path: org.apache.commons.net.ftp.*;
2.ftp default port is 21, if non-default port connection, specify: Ftp.connect (FTPHOSTADDR, 22);//22 as port number
3.ftp.changeworkingdirectory (Ftppath)//Implement switch directory
4.ftpfile[] fs = Ftp.listfiles (); Gets the list of files in the specified directory
public class Ftptools {private final static string ftphostaddr = "xxx";//server address private final static string Ftppath = "XXX";//Operating server directory private final static string ftpname = "xxx";//server login private final static string ftppwd = "xxx"; /Login Password Private final static String LocalPath = Getcurentcontentpath () + "uploadfiles"; Private final static String Fileseparator = System.getproperty ("File.separator"); private static final Logger Logger = Logger.getlogger (Ftptools.class); public static void Main (string[] args) {ftptools tools = new Ftptools (); Tools.downfile ("test.txt");} /** * Download files from File server to local * @param filename */public static void Downfile (String filename) {ftpclient ftp = initftp (); try{//4. Specify the directory to download ftp.changeworkingdirectory (ftppath);//Transfer to the FTP server directory//5. Traverse the Downloaded directory ftpfile[] fs = Ftp.listfiles (); for (Ftpfile ff:fs) {//Solve Chinese garbled problem, two decoding byte[] Bytes=ff.getname (). getBytes("Iso-8859-1"); String fn=new string (bytes, "UTF8"); if (fn.equals (filename)) {//6. write operation, write it to a local file Logger.info ("Download file:" +filename+ "Start:" + System.currenttimemillis ()); File LocalFile = new file (LocalPath +fileseparator+ ff.getname ()); OutputStream is = new FileOutputStream (localfile); Ftp.retrievefile (Ff.getname (), is); 7. Determine if the local file is written correctly, and if it is written correctly, delete the file on the file server if (Localfile.exists ()) {logger.info ("Delete file on server:" +filename); Ftp.deletefile (Ff.getname ()); } logger.info ("Download file:" +filename+ "End:" +system.currenttimemillis ()); Is.close (); }} ftp.logout (); }catch (Exception e) {e.printstacktrace (); New Exception ("error occurred while downloading files from server");} finally{if (ftp.isconnected ()) {TRy {ftp.disconnect (); } catch (IOException IoE) {ioe.printstacktrace (); }}}} public static FtpClient initftp () {int reply; ftpclient ftp = new ftpclient (); try {//1. Connect server//ftp.connect (FTPHOSTADDR); Ftp.connect (Ftphostaddr, 22);//2. Log on to the server If using the default port, you can use Ftp.connect (URL) to connect directly to the FTP server Ftp.login (ftpname, ftppwd);//3. Determine whether the login succeeded reply = Ftp.getreplycode (); if (! Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();}} catch (Exception e) {e.printstacktrace (); New Exception ("Server Connection Failed");} return FTP;} public static string Getcurentcontentpath () {String path = ""; Path = FtpTools.class.getClassLoader (). GetResource (""). ToString (); Path = Path.replace ("File:", ""). Substring (0, Path.indexof ("Web-inf")). Replace ("web-in", ""). Replace ("Web-i", "" "); return path; }}
Java Implementation ftp download file