Use FTP in Windows to transfer files between clients and servers

Source: Internet
Author: User

Recently, due to project requirements, I have to study FTP and share the code.

 

Public class ftputils {</P> <p>/** <br/> * upload files to the FTP server <br/> * @ Param IP <br/> * FTP Server IP address E. g: 192.168.8.22 <br/> * @ Param port <br/> * FTP server port <br/> * @ Param username <br/> * FTP Logon account <br/> * @ Param password <br/> * FTP logon password <br/> * @ Param serverpath <br/> * FTP server storage directory (relative path) default default missing time point to the main directory <br/> * @ Param file <br/> * absolute path of the file uploaded to the FTP server E. g: e:/log/log.txt or E:/log/log.txt <br/> * @ retur N <br/> * returns true if the call succeeds. Otherwise, false is returned. <br/> */<br/> Public Boolean uploadfile (string IP, int port, string username, <br/> string password, string serverpath, string file) {<br/> // indicates an upload failure <br/> Boolean success = false; <br/> // create an ftpclient object <br/> ftpclient FTP = new ftpclient (); <br/> FTP. setcontrolencoding ("UTF-8"); <br/> try {<br/> int reply = 0; <br/> // connect to the FTP server <br/> // if you use the default port, you can use ftp. connect (IP) Directly connect to the FTP server <br/> FTP. connect (IP, Port); <br/> // log on to FTP <br/> FTP. login (username, password); <br/> // check whether the returned value is reply> = 200 & reply <300 if yes, the logon is successful <br/> reply = FTP. getreplycode (); <br/> // The returned value starting with 2 is true <br/> If (! Ftpreply. ispositivecompletion (reply) {<br/> FTP. disconnect (); <br/> return success; <br/>}< br/> // go to the specified upload directory <br/> serverpath = gbktoiso8859 (serverpath ); <br/> FTP. changeworkingdirectory (iso8859togbk (serverpath); </P> <p> checkpathexist (FTP, iso8859togbk (serverpath )); </P> <p> // input stream <br/> inputstream input = NULL; <br/> try {<br/> file = gbktoiso8859 (File ); <br/> input = new fileinputstream (iso8859togbk (File); <br/>}catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/>}< br/> // store the uploaded file to the specified directory <br/> file = iso8859togbk (File ); <br/> flag = FTP. storefile (iso8859togbk (serverpath) + "/" + iso8859togbk (getfilename (File), input); <br/> // close the input stream <br/> input. close (); <br/> // exit FTP <br/> FTP. logout (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> If (FTP. iscon Nected () {<br/> try {<br/> FTP. disconnect (); <br/>}catch (ioexception IOE) {<br/>}< br/> return success; <br/>}</P> <p>/** <br/> * download files from the FTP server <br/> * @ Param IP <br /> * FTP Server IP address E. g: 192.168.8.22 <br/> * @ Param port <br/> * FTP server port <br/> * @ Param username <br/> * FTP Logon account <br/> * @ Param password <br/> * FTP logon password <br/> * @ Param serverpath <br/> * by default, the relative path on the FTP server is missing and points to the master File <br/> * @ Param filename <br/> * file name to be downloaded <br/> * @ Param localpath <br/> * The local path saved after download does not contain the file name <br/> * @ return <br/> * returns true if the call succeeds, otherwise, false is returned. <br/> */<br/> Public Boolean downfile (string IP, int port, string username, <br/> string password, string serverpath, string filename, <br/> string localpath) {<br/> // indicates that the download failed. <br/> Boolean success = false; <br/> // create an ftpclient object <br/> ftpclient FTP = new ftpc Lient (); <br/> FTP. setcontrolencoding ("UTF-8"); <br/> try {<br/> int reply; <br/> // connect to the FTP server <br/> // if the default port is used, you can use ftp. connect (IP) directly to the FTP server <br/> FTP. connect (IP, Port); <br/> // log on to FTP <br/> FTP. login (username, password); <br/> reply = FTP. getreplycode (); <br/> If (! Ftpreply. ispositivecompletion (reply) {<br/> FTP. disconnect (); <br/> return success; <br/>}< br/> // go to the specified download directory <br/> serverpath = gbktoiso8859 (serverpath ); <br/> FTP. changeworkingdirectory (this. iso8859togbk (serverpath); <br/> // list all files in the directory <br/> ftpfile [] FS = FTP. listfiles (); <br/> filename = This. gbktoiso8859 (filename); <br/> localpath = This. gbktoiso8859 (localpath); </P> <p> // traverses all files and finds the specified file. <br/> Fo R (ftpfile F: FS) {<br/> If (F. getname (). equals (iso8859togbk (filename) {<br/> // initialize the file according to the absolute path <br/> file localfile = new file (iso8859togbk (localpath) + "/" + F. getname (); <br/> file localfiledir = new file (iso8859togbk (localpath); <br/> // create if (! Localfiledir. exists () {<br/> localfiledir. mkdirs (); <br/>}< br/> // output stream <br/> outputstream is = new fileoutputstream (localfile ); <br/> // download the file <br/> flag = FTP. retrievefile (F. getname (), is); <br/> is. close (); <br/>}< br/> // exit FTP <br/> FTP. logout (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> If (FTP. isconnected () {<br/> try {<br/> FTP. disconnect (); <Br/>}catch (ioexception IOE) {<br/>}< br/> return success; <br/>}</P> <p>/** <br/> * Find whether the specified directory does not exist. <br/> * <br /> * @ Param ftpclient <br/> * FTP server to be checked by ftpclient <br/> * @ Param string <br/> * filepath directory to be searched <br/> * @ return <br/> * Boolean: yes: True, No: false <br/> * @ throws ioexception <br/> */<br/> private Boolean checkpathexist (ftpclient, string fil Epath) <br/> throws ioexception {<br/> Boolean existflag = false; <br/> try {<br/> If (filepath! = NULL &&! Filepath. Equals ("") {<br/> If (filepath. indexof ("/")! =-1) {<br/> int Index = 0; <br/> while (Index = filepath. indexof ("/"))! =-1) {<br/> If (! Ftpclient. changeworkingdirectory (filepath. substring (0, index) {<br/> ftpclient. makedirectory (filepath. substring (0, index); <br/>}< br/> ftpclient. changeworkingdirectory (filepath. substring (0, index); <br/> filepath = filepath. substring (index + 1, filepath. length (); <br/>}< br/> If (! Filepath. Equals ("") {<br/> If (! Ftpclient. changeworkingdirectory (filepath) {<br/> ftpclient. makedirectory (filepath); <br/>}< br/> existflag = true; <br/>}< br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return existflag; <br/>}</P> <p>/** <br/> * obtain the file name based on the absolute path <br/> * @ Param file <br/> * file absolute path E. g: E. g: e:/log/log.txt or E: // log // log.txt <br/> * @ return <br/> * transcoded file name <br/> */<br/> PRI Vate string getfilename (string file) {<br/> // file name <br/> string filename = ""; <br/> If (file! = NULL &&! File. equals ("") {<br/> file = file. replaceall (matcher. quotereplacement ("//"), "/"); <br/> string [] STRs = file. split ("/"); <br/> filename = STRs [STRs. length-1]; <br/>}< br/> filename = gbktoiso8859 (filename); // transcoding <br/> return filename; <br/>}</P> <p>/** <br/> * transcoding [ISO-8859-1-> GBK] <br/> * different platforms require different transcoding <br/> * @ Param OBJ <br/> * @ return <br/> */<br/> private string iso8859togbk (Object OBJ) {<br/> try {<br/> If (OBJ = NULL) <br/> return ""; <br/> else <br/> return new string (obj. tostring (). getbytes ("iso-8859-1"), "GBK"); <br/>} catch (exception e) {<br/> return ""; <br/>}</P> <p>/** <br/> * transcoding [GBK-> ISO-8859-1] <br/> * different platforms different transcoding needs <br/> * @ Param OBJ <br/> * @ return <br/> */<br/> private string gbktoiso8859 (Object OBJ) {<br/> try {<br/> If (OBJ = NULL) <br/> return ""; <br/> else <br/> return new string (obj. tostring (). getbytes ("GBK"), "iso-8859-1"); <br/>} catch (exception e) {<br/> return ""; <br/>}</P> <p>

Code Description:

The uploadfile () method is called when files are uploaded to the FTP server, and the downfile () method is called when files are downloaded from the FTP server.

 

Of course, to upload files to the FTP server, the FTP server requires write permission.

 

The jar to be used is a commons-net-2.0.jar.

Commons-net-2.0.jar API address: http://commons.apache.org/net/api/index.html

 

Hope to help friends!

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.