FTP File Download

Source: Internet
Author: User
Tags ftp file how to use ftp

FTP File Upload

FTP File Download

Delete an FTP File

Create directory for FTP File Operations

 

I have previously written an FTP-based file upload method. How can I not download an uploaded file? If only the upload is not downloaded, the upload is useless. So today I will learn how to use ftp to download files.

Once you know how to upload the file, it becomes very easy to download it. Uploading is to put the file on the server, while downloading is to take the file from the server. One is to read the file locally and then write it to the server; the other is to read the file from the server and then write it to the local. This is the basic principle. Let's take a look at the specificCode:

         ///   <Summary>          ///  FTP File Download  ///  </Summary>          ///   <Param name = "userid">  FTP User Name  </Param>          ///   <Param name = "PWD">  FTP Password  </Param>          ///   <Param name = "ftppath">  FTP file path  </Param>          ///   <Param name = "filepath"> Download and save path  </Param>          ///   <Param name = "FILENAME">  FTP file name  </Param>          ///   <Returns> </returns>          Public   String Download ( String Userid, String PWD, String Ftppath, String Filepath,String  Filename ){  String Sret = "  Download successful!  "  ; Ftpwebrequest reqftp;  Try  {Filestream outputstream = New Filestream (filepath + Filename, filemode. Create );  //  Create an ftpwebrequest object based on Uri Reqftp = (ftpwebrequest) ftpwebrequest. Create ( New Uri (ftppath + Filename ));  //  Specify the command to be executed Reqftp. method = Webrequestmethods. FTP. downloadfile;  //  Data transmission type Reqftp. usebinary = True  ; Reqftp. usepassive = False  ;  // FTP user name and password Reqftp. Credentials = New  Networkcredential (userid, PWD); ftpwebresponse response = (Ftpwebresponse) reqftp. getresponse ();  //  Write the downloaded file to the stream Stream ftpstream = Response. getresponsestream ();  Long CL = Response. contentlength;  //  Set the buffer size to 2 kb.                 Int Buffersize = 2048  ;  Int  Readcount;  Byte [] Buffer = New   Byte  [Buffersize];  //  2 kb of each file stream read Readcount = ftpstream. Read (buffer, 0  , Buffersize );  While (Readcount> 0  ){  //  Write content from the file stream Outputstream. Write (buffer, 0  , Readcount); readcount = Ftpstream. Read (buffer, 0  , Buffersize );}  //  Disable two streams and FTP connections  Ftpstream. Close (); outputstream. Close (); response. Close ();} Catch  (Exception ex) {sret = Ex. Message ;}  //  Returns the download result (whether the download is successful)              Return  Sret ;} 

 

The above code can implement a simple FTP download function, as long as you call this method as needed. The code is very simple and the function is very practical.

There are also many FTP-related operations that will be shared with you in the future. Stay tuned!

 

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.