JAVA in the use of ftpclient implementation file upload download __java

Source: Internet
Author: User
Tags ftp login server port

In the Java program, often need to deal with FTP, such as uploading files to the FTP server, download files, this article briefly describes how to use the FtpClient in the Jakarta Commons (in the Commons-net package) to achieve upload download files.

The jar packages used are:

Commons-net-1.4.1.jar

Jakarta-oro.jar

First, upload files to view the source print?

01 File Upload source code
02 /**
03 * Description: Uploading files to FTP server
04 * @Version1.0
05 * @param URL FTP server hostname
06 * @param port FTP server ports
07 * @param username FTP login Account
08 * @param password FTP login password
09 * @param path FTP server save directory
10 * @param filename uploaded to the FTP server
11 * @param input stream
12 * @return Returns True successfully, otherwise returns false
13 */
14 public static Boolean UploadFile (
15 String URL,//FTP Server hostname
16 int PORT,//FTP Server Port
17 String username,//FTP login account
18 String password,//ftp login password
19 String path,//ftp server save directory
20 String filename,//Upload file name on FTP server
21st InputStream input//input stream
22 ) {
23 Boolean success = false;
24 ftpclient ftp = new FtpClient ();
25 try {
26 int reply;
27 Ftp.connect (URL, port);//Connect FTP server
28 If you use the default port, you can connect to the FTP server directly using the Ftp.connect (URL) method
29 Ftp.login (username, password);/Login
30 Reply = Ftp.getreplycode ();
31 if (! Ftpreply.ispositivecompletion (Reply)) {
32 Ftp.disconnect ();
33 return success;
34 }
35 Ftp.changeworkingdirectory (path);
36 Ftp.storefile (filename, input);
37
38 Input.close ();
39 Ftp.logout ();
40 Success = true;
41 catch (IOException e) {
42 E.printstacktrace ();
43 finally {
44 if (ftp.isconnected ()) {
45 try {
46 Ftp.disconnect ();
47 catch (IOException IoE) {
48 }
49 }
50 }
51 return success;
52 }

The following is a file upload test case: Check the source print?

01 /**
02 * Upload the local file to the FTP server
03 *
04 */
05 public void Testuploadfromdisk () {
06 try {
07 FileInputStream in=new FileInputStream (New File ("D:/test.txt"));
08 Boolean flag = UploadFile ("127.0.0.1", "Administrator", "zyuc2011", "Test", "test.txt", in);
09 SYSTEM.OUT.PRINTLN (flag);
10 catch (FileNotFoundException e) {
11 E.printstacktrace ();
12 }
13 }
View the source print?
01 /**
02 * Generate a file on the FTP server and write a string to the file
03 *
04 */
05 public void testuploadfromstring () {
06 try {
07 String str = "This is the string to write." ";
08 InputStream input = new Bytearrayinputstream (str.getbytes ("Utf-8"));
09 Boolean flag = UploadFile ("127.0.0.1", "Administrator", "zyuc2011", "Test", "test.txt", input);
10 SYSTEM.OUT.PRINTLN (flag);
11 catch (Unsupportedencodingexception e) {
12 E.printstacktrace ();
13 }
14 }

Second, File download view source printing?

01 File Download Source code
02 /**
03 * Description: Downloading files from FTP server
04

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.