Commons NET implementation ftp upload download

Source: Internet
Author: User
Tags api manual ftp file response code

The recent project needs to upload to FTP files, select Commons net. The FTP tool class in Commons NET package can help us to easily ftp file upload/download. One of the most important classes is the FtpClient class, which provides a number of FTP-related

methods, such as links, logins, uploads, downloads, and logoff.

FTP operation of the process is generally connected to the server, login, file upload/download, file (directory) to add delete changes and other operations. Usually used more is the file upload and download.

Here are some basic upload operations (you can use Commons Net's jar bundle):

public class Ftputil {public static void main (string[] args) {ftpclient ftpclient = new FtpClient ();

			try {//connection specifies the server, the default port is Ftpclient.connect ("127.0.0.1");
			SYSTEM.OUT.PRINTLN ("Connect to Server");
			Gets the response string (can be set on the FTP server) string replystring = Ftpclient.getreplystring ();
			System.out.println ("replystring:" + replystring);
			Gets the response code to verify that the connection is successful int reply = Ftpclient.getreplycode (); if (!
				Ftpreply.ispositivecompletion (Reply)) {System.out.println ("");
			System.exit (1);
			
			//Set link encoding, Windows host UTF-8 will be garbled, need to use GBK or gb2312 encoding ftpclient.setcontrolencoding ("GBK");
			Login Server Boolean login = Ftpclient.login ("luojing", "luojing");
			if (login) {System.out.println ("Login successful!");
			else {System.out.println ("Login failed!");
			
			//Get the name of all files and folders ftpfile[] files = ftpclient.listfiles ();
				for (Ftpfile file:files) {if (File.isdirectory ()) {System.out.println (File.getname () + "folder"); } if (File.isfile ()) {System.out.prinTLN (File.getname () + "is a file");
			
			}//Generate InputStream for uploading local files inputstream in = new FileInputStream ("E:\\1.txt");
			Upload file Ftpclient.storefile ("Dest.txt", in);
			
			In.close ();
			Log Off Login Boolean logout = Ftpclient.logout ();
			if (logout) {System.out.println ("logout successful!");
			else {System.out.println ("Logoff failed!");

		} catch (Exception e) {e.printstacktrace ();
				Finally {//close link needs to be placed in the finally statement block if (ftpclient.isconnected ()) {try {ftpclient.disconnect ();
				catch (IOException e) {e.printstacktrace ();
 }
			}

		}
	}

}

In addition, some methods of File/folder manipulation are also provided in the FtpClient class. Through Commos NET provides the method, may realize the breakpoint transmission and so on conveniently the function. I can also use the same Retrievefilestream method to get an input stream from a specified file on a remote server for us to read manually, or to get an output stream object to upload to a file on a remote server using the Appendfilestream method. Then we can manually read the data from the local file and write to the remote service, for example, we want to know the progress of the upload. In general, Commons Net offers a very good, convenient approach. Some of the other features need to look at the API manual when used. may be the environmental impact, found that after the company's learning efficiency than in the school a lot of high, continue to refuel.

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.