Java Client calls FTP upload download file

Source: Internet
Author: User
Tags ftp login

1:java client uploads, downloads files.

 Packagecom.li.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;/*** FTP upload Download Tool class * <p>Title:FtpUtil</p> * <p>description: </p> * <p>company:www.itcast.c Om</p> *@authorinto Yunlong * @date July 29, 2015 afternoon 8:11:51 *@version1.0*/ 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;  } ftp.enterlocalpassivemode (); //set FTP to passive mode. Otherwise it will not succeed.            Active mode because the client firewall blocks server 20 ports. //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 ("D:\\data.txt")); BooleanFlag = UploadFile ("192.168.100.91", +, "Liyafei", "1367356", "/home/liyafei/myfile", "/file", "Data.txt", in);          SYSTEM.OUT.PRINTLN (flag); } Catch(FileNotFoundException e) {e.printstacktrace (); }      }}

2:

FTP active mode and passive mode: https://www.cnblogs.com/ajianbeyourself/p/7655464.html

Java Client calls FTP upload 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.