Application of FTP upload and download files

Source: Internet
Author: User
Tags ftp login response code file transfer protocol

The FTP (file Transfer Protocol) protocol is primarily used for file transfer on the network. FTP Communication In addition to a default port 21, there are other ports, the same city two ports at the same time data transmission. One is the default port (usually 21), the main control connection, that is, the command protocol and server-side response code transmission. Another non-standard port is the main data, upload download files and so on. Specific upload, download code as follows

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;/*** 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:/2017/04/03. 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 in Put) {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 ("E:\\image\\eason.jpg")); BooleanFlag = UploadFile ("192.168.242.135", +, "Ftpuser", "Ftpuser", "/home/ftpuser/www/images", "/2017/04/03", "eason.jpg", in);          SYSTEM.OUT.PRINTLN (flag); } Catch(FileNotFoundException e) {e.printstacktrace (); }      }

Application of FTP upload and download files

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.