[Java] uses Apache's commons-net library for FTP operations

Source: Internet
Author: User
Tags deprecated

Because the FTP operation is needed in the recent work, and there is no existing FTP code on hand. Went online to find a bit, found that everyone using Apache Commons-net Library in the ftpclient.

However, the feeling is not very convenient to use. And found a lot of packages on the Internet. I think it's not very useful. So I wrote one of my own. Most of the examples on the web are directly manipulating files, and what I need more is to read the memory or write it from memory. And there are a lot of practical singleton patterns, but I think if the call is more, there may be problems.

1 /**2 * Some FTP operations are encapsulated3  */4 ImportJava.io.BufferedReader;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.InputStreamReader;8 ImportJava.io.OutputStream;9 ImportJava.io.OutputStreamWriter;Ten  One ImportOrg.apache.commons.net.ftp.FTP; A Importorg.apache.commons.net.ftp.FTPClient; - ImportORG.APACHE.COMMONS.NET.FTP.FTPCMD; - Importorg.apache.commons.net.ftp.FTPReply; the ImportOrg.apache.log4j.Logger; -  -  Public classFtputil { -     Private StaticLogger Logger = Logger.getlogger (ftputil.class); +  -     Private Staticftpclient getconnection () { +FtpClient client =Newftpclient (); AClient.setdatatimeout (30000); atClient.setdefaulttimeout (30000); -         returnclient; -     } -  -      Public StaticFtpClient getconnection (String host)throwsIOException { -FtpClient client =getconnection (); in Client.connect (host); -         if(!ftpreply.ispositivecompletion (Client.getreplycode ())) { to             Throw NewIOException ("Connect Error"); +         } -         returnclient; the     } *      Public StaticFtpClient getconnection (String host,intPortthrowsIOException { $FtpClient client =getconnection ();Panax Notoginseng Client.connect (host, port); -         if(!ftpreply.ispositivecompletion (Client.getreplycode ())) { the             Throw NewIOException ("Connect Error"); +         } A         returnclient; the     } +  -     StaticFtpClient getconnection (string host, string username, string password)throwsIOException { $FtpClient client=getconnection (host); $ client.login (username, password); -         if(!ftpreply.ispositivecompletion (Client.getreplycode ())) { -             Throw NewIOException ("Login Error"); the         } -         returnclient;Wuyi     } the  -     StaticFtpClient getconnection (String host,intport, string Username, string password) Wu             throwsIOException { -FtpClient client =getconnection (host, port); About client.login (username, password); $         if(!ftpreply.ispositivecompletion (Client.getreplycode ())) { -             Throw NewIOException ("Login Error"); -         } -         returnclient; A     } +  the     /** - * Move the file (if the target file exists then do not move and return false) $      */ the     Static BooleanmoveFile (String curfilename, String targetfilename, ftpclient client) the             throwsIOException { the         intreply; theReply =Client.sendcommand (FTPCMD.RNFR, curfilename); -         if(Ftpreply.isnegativepermanent (Reply)) { in             //logger.error ("FTP Move file error. Code: "+ Reply); theSystem.out.println ("FTP Move file error. Code: "+reply); the             return false; About         } theReply =Client.sendcommand (Ftpcmd.rnto, targetfilename); the         if(Ftpreply.isnegativepermanent (Reply)) { the             //logger.error ("FTP Move file error. Code: "+ Reply); +System.out.println ("FTP Move file error. Code: "+reply); -             return false; the         }Bayi         return true; the     } the  -     /** - * Read File list the      */ the @Deprecated the     StaticString[] Getfilenamelist (ftpclient client)throwsIOException { theString names[] =client.listnames (); -         if(!ftpreply.ispositivecompletion (Client.getreply ())) { the             Throw NewIOException ("Get File Name list error"); the         } the         returnnames;94     } the  the     /** the * Read File98      */ About     StaticString ReadFile (string path, FtpClient client)throwsIOException { - Client.setfiletype (ftp.ebcdic_file_type);101BufferedReader BF =NewBufferedReader (102                 NewInputStreamReader (Client.retrievefilestream (path), "UTF-8"));103StringBuilder SB =NewStringBuilder ();104 String str; the          while(str = bf.readline ())! =NULL) {106Sb.append (str). append ("\ n");107         }108         returnsb.tostring ();109     } the 111 @Deprecated the     Static Booleandownfile (String RemotePath, String LocalPath, ftpclient client)113             throwsIOException { theFileOutputStream fos =NewFileOutputStream (localPath); the Client.setfiletype (ftpclient.binary_file_type); the client.retrievefile (RemotePath, FOS);117         return false;118     }119  -     /**121 * Write Files122      */123     Static BooleanStoreasfile (String context, string RemotePath, ftpclient client)124             throwsIOException { theOutputStream out =Client.storefilestream (remotepath);126OutputStreamWriter writer =NewOutputStreamWriter (out, "UTF-8");127 writer.write (context); - Writer.flush ();129 writer.close (); the         return true;131     } the 133     Static voidClose (ftpclient client) {134         Try {135             if(Client! =NULL) {136 Client.disconnect ();137             }138}Catch(IOException e) {139  $         }141     }142}
Ftputil

[Java] uses Apache's commons-net library for FTP operations

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.