Use the ftp4j class for FTP operations

Source: Internet
Author: User
Tags remote ftp server ftp client

Ftp4j is an FTP client Java class library that implements most of the functions that the FTP client should have. You can embed ftp4j into your Java application to transfer files (including upload and download), browse directories and files on remote FTP servers, create, delete, and retry, move remote directories and files. Ftp4j provides multiple methods to connect to a remote FTP server, including direct connection through TCP/IP, connection through FTP Proxy, HTTP proxy, socks4/4A proxy and SOCKS5 proxy, and secure connection through SSL.

Simple Application-API Introduction

The main class of the library is ftpclient (it. sauronsoftware. ftp4j. ftpclient ).

1. Create an ftpclient instance
Ftpclient client = new ftpclient ();

Connect to the specified FTP Server (domain name or IP address) without specifying a port, use the default port 21
Client. Connect ("ftp.host.com",/* 21 */);

Logon Verification
Client. login ("user", "pswd ");

The following is an anonymous logon
// Client. login ("anonymous", "arbitrary password settings ");
Client. login ("anonymous", "ftp4j ");

Exit safely
Client. Disconnect (true );
// Force exit
// Client. Disconnect (false );

File and folder operations:

Get current folder
String dir = client. currentdirectory ();

Change current folder
Client. changedirectory (newpath );

// Absolute path
// Client. changedirectory ("/AN/absolute/One ");
// Relative path
// Client. changedirectory ("relative ");

// Roll back to the parent directory
Client. changedirectoryup ();

// Rename a file or folder
Client. Rename ("oldname", "newname ");

// Move a file or folder
Client. Rename ("myfile.txt", "myfolder/myfile.txt ");

// Delete an object
Client. deletefile (relativeorabsolutepath );
// Client. deletefile ("useless.txt ");

// Create a directory
Client. createdirectory ("newfolder ");

// Delete the directory (empty directory)
Client. deletedirectory (absoluteorrelativepath );
// Client. deletedirectory ("oldfolder ");

// Browse files
Ftpfile [] list = client. List ();

// Use wildcard to browse files
Ftpfile [] list = client. List ("*. jpg ");

// Display the modification time of a file or folder
Java. util. Date MD = client. modifieddate ("filename. Ext ");

// Upload and download files
// Download remotefile. Ext from the server to the local localfile. ext
Client. Download ("remotefile. Ext", new java. Io. File ("localfile. Ext "));

// Upload local localfile. Ext to the current directory
Client. Upload (New java. Io. File ("localfile. Ext "));

// Listen to the file transmission status when uploading and downloading files
Public class mytransferlistener implements ftpdatatransferlistener {

// Triggered when the file starts to be uploaded or downloaded
Public void started (){
}
// Display the number of transmitted bytes
Public void transferred (INT length ){
}
// Triggered when file transfer is completed
Public void completed (){
}
// Triggered when the transfer is abandoned
Public void aborted (){
// Transfer aborted
}
// Triggered when transmission fails
Public void failed (){
}

Example:
Client. Download ("remotefile. Ext", new java. Io. File ("localfile. Ext"), new mytransferlistener ());
Client. Upload (New java. Io. File ("localfile. Ext"), new mytransferlistener ());

// Ftp4j supports resumable data transfer.
The following is a simple example: * parameter 1056 skips the 1056-byte download.
Client. Download ("remotefile. Ext", new java. Io. File ("localfile. Ext"), 1056 );

Set Transmission Mode
// ASC code
Client. settype (ftpclient. type_textual );
// Binary
Client. settype (ftpclient. type_binary );
// Automatically select (based on the file content)
Client. settype (ftpclient. type_auto );

// Set the connector
Client. setconnector (connector );
SSL socket connection
Client. setconnector (It. sauronsoftware. ftp4j. connectors. sslconnector)

 
Client. setconnector (anyconnectoryouwant );

Ftp4j Official Website: http://www.sauronsoftware.it/projects/ftp4j/

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.