Simple operation of FTP under the Org.apache.commons.net tool

Source: Internet
Author: User

/** * Connect FTP Server * @param IPIP address * @param port port number * @param username username * @param password password * @param ftppathftp subdirectory * @throws IOException */public int connectserver (string ip, int port, string username,string Password, string ftppath) throws Ioexce ption {System.out.println (this, "=============all_attributes===========" + "Ip_" +ip+ "Port_" +port+ "username_" + Username+ "Password_" +password+ "Ftppath_" +ftppath); int Reply;int Connectflag = 1;try {ftpclient.connect (IP, port);// Connect the FTP server//If using the default port, you can use Ftp.connect (URL) to connect directly to the FTP server Ftpclient.login (username, password); Reply = Ftpclient.getreplycode (); if (!  Ftpreply.ispositivecompletion (Reply)) {ftpclient.disconnect (); connectflag = 0; return connectflag;} Ftpclient.changeworkingdirectory (Ftppath);//Transfer to FTP server directory System.out.println (this, "=============connet_ftp_is_ok_ Falg_ "+connectflag+" =========== ");} catch (Exception e) {System.out.println (this, "=============connet_ftp_is_error" +connectflag+ "===========");} return Connectflag;} ---------------------------------------------------------------------------------------------------------------------------------- ------/** * Upload upload file * * @throws java.lang.Exception * @return-1 file does not exist-2 file content is empty >0 successfully uploaded, returns the size of the file * @param newname New file name after pass * @param filePath the path of the uploaded file */public long upload (string localPath, String newfilename) throws Exception {long resu lt = 0;try {File File = new file (LocalPath), if (!file.exists ()) {result = -1;//file does not exist}if (file.length () = = 0) {result =-2; The contents of the file are empty}//result = File.length (); is = new FileInputStream (file); Ftpclient.storefile (NewFileName, is); Ftpclient.logout (); System.out.println (This, "=========" +newfilename+ "File Upload succeeded ==========");} catch (Exception e) {e.printstacktrace (); System.out.println (This, "=========" +newfilename+ "File upload failed ==========");} finally{} return result; ---------------------------------------------------------------------------------/** * Upload via file path * @param filePath * @ return * @throws Exception */public long upload (String filePath) throws Exception {String newname = "", if (Filepath.indexof ("/") >-1) {newname = filepath.substring (Filepath.lastindexof ("/") + 1); } else {newname = FilePath;} return upload (FilePath, newname);} ----------------------------------------------/** * * closeserver * * Disconnect the link to the FTP server * * @throws java.io.IOException */pu Blic void CloseServer () throws Ioexception{try {if (is = null) {Is.close ();} if (ftpclient! = null) {Ftpclient.disconnect ();}} catch (IOException e) {e.printstacktrace ();}}     -------------------------------------------------/** * Reads a text file.        * */public static string Readtxtfile (File filepathandname) {string read;        FileReader Fileread;            try {fileread = new FileReader (filepathandname);            Bufread = new BufferedReader (fileread);                try {while ((read = Bufread.readline ()) = null) {Readstr = readstr + read+ "\ r \ n"; }} catch (IOException e) {//TODO Auto-genErated Catch block E.printstacktrace ();         }} catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace ();        } System.out.println ("File content is:" + "\ r \ n" + readstr);    return readstr;     } Write Files------------------------------------------------------/** *. * */public static void Writetxtfile (String newstr,file filepathandname) throws ioexception{//Read the original file contents first, then        Write operation String Filein = newstr + "\ r \ n" + readstr + "\ r \ n";        Randomaccessfile mm = null;            try {mm = new Randomaccessfile (filepathandname, "RW");        Mm.writebytes (Filein);        } catch (IOException E1) {//TODO automatically generate catch block E1.printstacktrace ();                } finally {if (mm! = null) {try {mm.close (); } catch (IOException E2) {//TODO automatically generate catch block E2.printstacKtrace (); }}}}----------------------------------------------------/** * * downloadFile * * Download Resources for FTP server * @ param remotefilename To download file name * @param localdires Store path * @param ftppath FTP server to store subdirectories of files to download * * @throws JAVA.IO.IOEXC        Eption */public void DownloadFile (String remotefilename,string localdires, String ftppath) throws ioexception{    String strFilePath = localdires + "/" + remotefilename;           Write address file save path bufferedoutputstream BF = null;               Boolean success=false;            try {ftpclient.changeworkingdirectory (Ftppath);                        bf= New Bufferedoutputstream (New FileOutputStream (strFilePath));                        System.out.println (This, "=========" +remotefilename+ "Start download ==========");  Success = Ftpclient.retrievefile (REMOTEFILENAME,BF);            Download operation//Ftpclient.logout (); if (success==true) {System.out.println (this, "=========" +remotefilename+"Successfully downloaded to ==========" +localdires);        }} catch (IOException E1) {//TODO automatically generate catch block E1.printstacktrace ();                    } finally {if (null! = BF) {try {bf.flush ();                Bf.close ();                } catch (IOException E2) {//TODO automatically generate catch block E2.printstacktrace (); }}} if (Success=false) {System.out.println (this, "=========" +remotefilename+         "Download failed =========="); }    }        -----------------------------------------

Simple operation of FTP under the Org.apache.commons.net tool

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.