Using the Ftp4j class to implement FTP operations

Source: Internet
Author: User
Tags ftp client

FTP4J is an FTP client Java class Library that implements most of the functionality that the FTP client should have. You can embed ftp4j into your Java application to transfer files (including uploads and downloads), browse directories and files on remote FTP servers, create, delete, rename, move remote directories and files. FTP4J provides a variety of ways to connect to remote FTP servers, including: direct connection via TCP/IP, connection via FTP proxy, HTTP proxy, socks4/4a proxy, and SOCKS5 proxy via SSL secure connection. FTP4J is a basic class library.


Introduction to Simple Application---API

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

1. Create a FtpClient instance
FtpClient client = new FtpClient ();

Connection to the specified FTP server (domain name or IP) does not specify a port, the default port 21 is used
Client.connect ("ftp.host.com",/*21*/);

Login Verification
Client.login ("User", "pswd");

The following is an anonymous login
Client.login ("Anonymous", "Password arbitrarily set");
Client.login ("Anonymous", "ftp4j");

Safe exit
Client.disconnect (TRUE);
Force exit
Client.disconnect (FALSE);

File and folder operations:

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

Change the current folder
Client.changedirectory (NewPath);

Absolute path
Client.changedirectory ("/an/absolute/one");
Relative path
Client.changedirectory ("relative");

Fallback to 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");

deleting files
Client.deletefile (Relativeorabsolutepath);
Client.deletefile ("Useless.txt");

Create a Directory
Client.createdirectory ("NewFolder");

Delete directory (empty directory)
Client.deletedirectory (Absoluteorrelativepath);
Client.deletedirectory ("Oldfolder");

Browse files
ftpfile[] list = Client.list ();

Browse files using a wildcard
ftpfile[] List = client.list ("*.jpg");

Show when a file or folder was modified
Java.util.Date MD = client.modifieddate ("filename.ext");


Uploading and downloading files
Download the Remotefile.ext on the server to the local Localfile.ext
Client.download ("Remotefile.ext", New Java.io.File ("Localfile.ext"));

Upload local localfile.ext to current directory
Client.upload (New Java.io.File ("Localfile.ext"));

Listen for file transfer status when uploading and downloading files
public class Mytransferlistener implements Ftpdatatransferlistener {

Triggered when the file starts uploading or downloading
public void started () {
}
Shows the number of bytes that have been transferred
public void transferred (int length) {
}
When the file transfer is complete, trigger
public void completed () {
}
Trigger when transfer is discarded
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 also supports the ability to continue the breakpoint
Here is a simple example: * parameter 1056 skip 1056 byte point download
Client.download ("Remotefile.ext", New Java.io.File ("Localfile.ext"), 1056);


Set Transfer Mode
ASC Code
Client.settype (ftpclient.type_textual);
Binary
Client.settype (ftpclient.type_binary);
Automatic selection (based on file contents)
Client.settype (Ftpclient.type_auto);

Setting up Connectors
Client.setconnector (connector);
SSL Socket Connection
Client.setconnector (it.sauronsoftware.ftp4j.connectors.SSLConnector)

Client.setconnector (anyconnectoryouwant);


FTP4J The project homepage:
http://www.sauronsoftware.it/projects/ftp4j/
http://www.sauronsoftware.it/projects/ftp4j/manual.php


Generally speaking, the FTP tool under test in Windows, generally under Linux, because Windows does not have too much permission control. And Linux control is very strict, often because no permissions to cause the operation failed, at this time, to the Linux FTP users to read and write permissions to correctly execute the above code.




This article is from the "Old GE Technology Learning" blog, please be sure to keep this source http://ptghb.blog.51cto.com/3902848/1665834

Using the Ftp4j class to implement FTP operations

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.