Java ftp file upload details

Source: Internet
Author: User
Tags file upload ftp ftp file server port

Let's take a look at two simple codes.

Upload the local file to the ftp server. The code is as follows:

@ Test
Public void testuploadfromdisk (){
Try {
Fileinputstream in = new fileinputstream (new file ("d:/test.txt "));
Boolean flag = uploadfile ("127.0.0.1", 21, "test", "test", "d:/ftp", "test.txt", in );
System. out. println (flag );
} Catch (filenotfoundexception e ){
E. printstacktrace ();
  }
 }

Generate a file on the ftp server and write a string to the file.

 

@ Test
Public void testuploadfromstring (){
Try {
Inputstream input = new bytearrayinputstream ("test ftp". getbytes ("UTF-8 "));
Boolean flag = uploadfile ("127.0.0.1", 21, "test", "test", "d:/ftp", "test.txt", input );
System. out. println (flag );
} Catch (unsupportedencodingexception e ){
E. printstacktrace ();
  }
 }

Complete instance

Import java. io. file;
Import it. sauronsoftware. ftp4j. ftpclient;
Import it. sauronsoftware. ftp4j. ftpfile;

Public class javaftp {
Private ftpclient client = null;
Public void init (){
Try {
Client = new ftpclient ();
Client. connect ("192.168.1.248", 21 );
Client. login ("db2inst1", "db2inst1 ");
} Catch (exception e ){
E. printstacktrace ();
}
}
Public static void main (string [] args ){
Try {
Javaftp = new javaftp ();
Javaftp. init ();
Javaftp. download ("e: // test", "/test /");
Javaftp. close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
Public void download (string localpath, string clientpath ){
Try {
If (clientpath! = Null & clientpath. length ()> = 0 ){
Client. changedirectory (clientpath );
}
Ftpfile [] list = client. list ();

For (int I = 0; I <list. length; I ++ ){
Ftpfile file = list [I];
System. out. println (file. gettype ());
If (file. gettype () = ftpfile. type_directory ){
File temp = new file (localpath + file. separator + file. getname ());
If (! Temp. exists ()){
Temp. mkdir ();
}
Download (localpath + file. separator + file. getname (), file. getname ());
}
Else if (file. gettype () = ftpfile. type_file ){
File localfile = new file (localpath + file. separator + file. getname ());
Client. download (file. getname (), localfile );
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}
Public void close (){
Try {
Client. disconnect (true );
} Catch (exception e ){
E. printstacktrace ();
}
}
}

Download files from the ftp server
 

* @ Version1.0 jul 27,200 8 5:32:36 by Cui hongbao (cuihongbao@d-heaven.com) creation
* @ Param url ftp server hostname
* @ Param port: ftp server port
* @ Param username ftp logon account
* @ Param password ftp logon password
* @ Param remotepath relative path on the ftp server
* @ Param filename the name of the file to be downloaded
* @ Param localpath: The local path saved after downloading
* @ Return
*/
Public static boolean downfile (string url, int port, string username, string password, string remotepath, string filename, string localpath ){
Boolean success = false;
Ftpclient ftp = new ftpclient ();
Try {
Int reply;
Ftp. connect (url, port );
// If the default port is used, you can use ftp. connect (url) to directly connect to the ftp server.
Ftp. login (username, password); // log on
Reply = ftp. getreplycode ();
If (! Ftpreply. ispositivecompletion (reply )){
Ftp. disconnect ();
Return success;
   }
Ftp. changeworkingdirectory (remotepath); // transfer to the ftp server directory
Ftpfile [] fs = ftp. listfiles ();
For (ftpfile ff: fs ){
If (ff. getname (). equals (filename )){
File localfile = new file (localpath + "/" + ff. getname ());
     
Outputstream is = new fileoutputstream (localfile );
Ftp. retrievefile (ff. getname (), is );
Is. close ();
    }
   }
   
Ftp. logout ();
Success = true;
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (ftp. isconnected ()){
Try {
Ftp. disconnect ();
} Catch (ioexception ioe ){
    }
   }
  }
Return success;
 }

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.