Java FTP download file

Source: Internet
Author: User
Tags ftp login server port

1, the use of official regular jar

Commons-net-1.4.1.jar

Jakarta-oro-2.0.8.jar

Note: Downloading files from Windows Server using FTP is not the same as downloading files from a Linux server

2. download files from Linux server with FTP

System.out.println (NewDate () + "Start entering Ftpdownload timer"); //FTP Server login CredentialsString host=propertiesmanager.getproperty ("Ftphost"); intPort=integer.parseint (Propertiesmanager.getproperty ("FtpPort")); String User=propertiesmanager.getproperty ("Ftpuser"); String Password=propertiesmanager.getproperty ("FTPPassword"); //Get Time field informationSimpleDateFormat sdf1=NewSimpleDateFormat ("YyyyMMdd"); SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyy-mm-dd"); Date Date=NewDate (); String Today1=Sdf1.format (date); String Today=Sdf.format (date); String txtfiledir= "/"; String Txtsavedir= "e:/datacenter/shengzhan/"; //Check if the local disk directory exists TXT file        BooleanFlag =Istxtexit (Today1,txtsavedir); System.out.println (NewDate () + "Determine if the TXT file exists:" +flag); Flagutil.ftpdownloadrunning=true; //txt-speaking download operations and parsing operations are divided into 2 separate operations to eliminate interference between each other.        if(flag==false)//file does not exist for FTP download operation{ftpclient ftp=NULL; Try            {                //data download for FTPftp=Newftpclient ();                   Ftp.connect (host, Port);                Ftp.login (user, password);                                Ftp.setfiletype (Ftpclient.binary_file_type); //setting up a Linux environmentFtpclientconfig conf =NewFtpclientconfig (Ftpclientconfig.syst_unix);                                Ftp.configure (conf); //determine if the connection is successful                intReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply))                    {Ftp.disconnect (); System.out.println ("FTP server refused connection."); return; }                                //set access Passive modeFtp.setremoteverificationenabled (false);                                                Ftp.enterlocalpassivemode (); //Retrieve all files in FTP directory and filter using time string                BooleanDIR =ftp.changeworkingdirectory (Txtfiledir); if(dir) {Ftpfile[]fs=Ftp.listfiles ();  for(Ftpfile f:fs) {if(F.getname (). IndexOf (today1+ ") >0) {System.out.println (NewDate () + "ftpdownload timer download txt Success"); File LocalFile=NewFile (txtsavedir+f.getname ()); OutputStream iOS=NewFileOutputStream (LocalFile);                                Ftp.retrievefile (F.getname (), iOS);                                 Ios.close ();  Break; }                        }                }            }             Catch(Exception e) {e.printstacktrace (); System.out.println (NewDate () + "FTP Download TXT file error"); }            finally            {                if(FTP! =NULL)Try{Ftp.disconnect ();}Catch(IOException IoE) {}}

3. download files from Windows Server using FTP

 Public Static Booleandownfile (String URL,//FTP server hostname            intPort//FTP Server PortString username,//FTP login AccountString Password,//FTP Login PasswordString RemotePath,//relative paths on the FTP serverString FileName,//the file name to downloadString LocalPath//save to Local path after download            ) {            BooleanSuccess =false; FtpClient FTP=Newftpclient (); Try {                intreply;                Ftp.connect (URL, port); //If you use the default port, you can connect directly to the FTP server using Ftp.connect (URL)Ftp.login (username, password);//LoginReply =Ftp.getreplycode (); if(!ftpreply.ispositivecompletion (Reply))                    {Ftp.disconnect (); returnsuccess; } System.out.println ("AAA"); Ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directoryftpfile[] fs =Ftp.listfiles ();  for(Ftpfile ff:fs) {System.out.println ("BB" +FS); if(Ff.getname (). Equals (FileName)) {System.out.println ("DD"); File LocalFile=NewFile (localpath+ "/" +ff.getname ()); OutputStream is=NewFileOutputStream (LocalFile);                      Ftp.retrievefile (Ff.getname (), is); System.out.println ("CCC" +ff.getname () +fileName);                    Is.close ();                }} ftp.logout (); Success=true; } Catch(IOException e) {e.printstacktrace (); } finally {                if(ftp.isconnected ()) {Try{ftp.disconnect (); } Catch(IOException IoE) {}}} returnsuccess; }

Java FTP download file

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.