- How to use:
- FtpClient client = new ftpclient ();
- Client.connect ("ftp.host.com", 8021);
- Client.login ("Carlo", "MyPassword");
- No exception indicates a successful connection, otherwise the mat throws a Ftpexception exception.
- Of course, there are some overloaded methods, if the port is 21 can be used Client.connect (host);
- In order to handle anonymous login, you can Client.login ("Anonymous", "ftp4j"), the user name is anonymous, the password is arbitrary.
- After logging in, you need to operate the server, such as directory display, entry, etc.;
- String dir = client.currentdirectory (); Show current directory
- Modify the entered directory
- Client.changedirectory (NewPath);
- You can use a relative path, or you can use an absolute path, as in the following two types.
- Client.changedirectory ("/an/absolute/one");
- Client.changedirectory ("relative");
- If you only want to go to the previous layer: Client.changedirectoryup ();
- There are two special directories in the ftp4j. ",".. ", respectively, representing the root directory and the previous level directory.
- Rename: Client.rename ("Oldname", "newname");
- or move files: Client.rename ("MyFile.txt", "Myfolder/myfile.txt");
- Delete file: Client.deletefile (Relativeorabsolutepath); You can use a relative or absolute path.
- Create directory: Client.createdirectory ("NewFolder");
- Delete directory: Client.deletedirectory (Absoluteorrelativepath);
- Show files, directories, links
- Because of the different server, the format of the returned file list is also different, possibly UNIX, or win. FTP4J Directory support:
- UNIX style and variants (i.e. MAC style)
- DOS style
- NetWare Styles
- Eplf
- MLSD
- . Displays the current catalog file: ftpfile[] list = Client.list ();
- Ftpfile contains the information you need, from type to whether it is a directory or a file, or a link.
- In addition to filtering:
- ftpfile[] List = Client.list ("*.jpg"); filter, show only pictures
- If you want to know how to modify the previous period, you need to get it separately, not from the list () method above:
- Java.util.Date MD = client.modifieddate ("filename.ext");
- The next step is to upload and download:
- Client.download ("remotefile.ext", new java.io.File ("Localfile.ext"));
- Client.upload (new java.io.File ("Localfile.ext"));
- Upload of additional content
- Client.append (new java.io.File ("Localfile.ext"));
- These are blocking methods that are returned only after completion, so a ftpclient can only be transferred at the same time. If you want multiple files to be transferred simultaneously, you can use multiple ftpclient,ftpclient to connect to the server separately.
- Today's software is not just functional implementation, but also pay attention to the user experience, so upload download need to know the progress, ftp4j through Ftpdatatransferlistener this interface to achieve, if there is a method:
- Public void transferred (int length) {
- Yet other length bytes have been transferred since the last time this
- This method is said to be in the distance from the last transmission to the content of this transmission,
- If you want to have a progress bar, need to know the total amount, and then the current amount of transmission, so need to do some processing, such as at the beginning of the upload, get the local file upload size, and then from this interface to get the transferred files superimposed to get the total number of files transferred, and then calculate progress.
- From ftpclient you can know: on upload: listener.transferred (l); L represents the amount of this transmission, but it doesn't seem to be what we want,
- Modified to: listener.transferred (done); This do represents the total amount of uploads, which is already there, and is good to use directly.
- }
- You can then add this listener client.download ("Remotefile.ext", new java.io.File ("Localfile.ext") by overloading the method, New Mytransferlistener ());, others slightly.
- For the same ftpclient, operations between multiple threads are affected, such as the ability to invoke Abortcurrentdatatransfer () in other threads to knot the current transport behavior.
- This method passes false to indicate a sudden stop, and passing in true indicates that sending the Abor command is a legitimate end. Then there will be ftpabortedexception abnormalities.
- FTP4J also supports breakpoints:
- Client.download ("Remotefile.ext", new java.io.File ("Localfile.ext"), 1056); expression starting from 1057 , the front does not need to transfer again.
- Uploads are similar.
- Active vs. Passive transmission:
- Active transport Behavior:
- The client sends the IP address and port number to the server
- Client requests the server to transfer data and start listening ports
- Server connection address and port
- The data transfer is initiated by a new channel.
- Proactive need for client support to receive information if the client has a firewall, proxy, etc., there will be some problems,
- Passive transfer behavior:
- The client requests the server to prepare for passive data transfer.
- The server replies with IP and port.
- The client request transmission is also connected.
- The data transfer is initiated by a new channel.
- Change in the following ways:
- Client.setpassive (false); //Active mode
- Client.setpassive (true); //Passive mode
- The default is passive.
- These properties can be set by System properties:
- such as Ftp4j.activeDataTransfer.acceptTimeout set timeout time:
- java-dftp4j.activedatatransfer.hostaddress=178.12. 34.167
- -dftp4j.activedatatransfer.portrange=6000-7000
- -dftp4j.activedatatransfer.accepttimeout=MyClass
- Set property values directly in the code. i.e.:
- System.setproperty ("ftp4j.activeDataTransfer.hostAddress", "178.12.34.167");
- System.setproperty ("Ftp4j.activeDataTransfer.portRange", "6000-7000");
- System.setproperty ("Ftp4j.activeDataTransfer.acceptTimeout", "5000");
- Binary and text transfer types:
- From the source can be seen after the prefix name to determine the type of file, so personally think, uploaded files need to know what type, accidentally changed the binary file to txt suffix, there may be some problems.
- Client.settype (ftpclient.type_textual);
- Client.settype (ftpclient.type_binary);
- Client.settype (Ftpclient.type_auto);
- Of course it provides some custom types of Client.settextualextensionrecognizer (Myrecognizer);
- Some servers support data compression transmission, called Mode Z. Can be passed client.setcompressionenabled (true); Open, before this can be confirmed:boolean compressionenabled = Client.iscompressionenabled ();
- Site Specifications and commands:
- ftpreply reply = Client.sendsitecommand ("YOUR COMMAND");
- You can also customize the command
- ftpreply reply = Client.sendcustomcommand ("YOUR COMMAND");
- Returns a Ftpreply object. You can get some return code, information, etc. from this object.
- -----------------------------------------------
- Above is some knowledge of ftp4j's documentation. With this knowledge, it is relatively easy to develop an Android client.
- If I want to get the file directory operation of the server (select file, select directory, enter directory)
- You can use Listview+listadapter to display directories and files, and then because of the Android operation you can use Onitemlongclicklistener long press and short press Onitemclicklistener to specify some actions separately, As a short press you can choose to enter the directory:
- Public void Onitemclick (adapterview<?> adapterview, view view, int i, long l) {
- Ftpfile Ftpfile=mfilelist.get (i);
- String Filename=ftpfile.getname ();
- if (".."). Equals (filename) {
- LOG.D (TAG, "Change directory up.");
- Changedirectoryup ();
- } Else if (".". Equals (filename) {
- LOG.D (TAG, "Change Directory root");
- Changedirectory ("/");
- } Else {
- If (Ftpfile.gettype () ==ftpfile.type_directory) {
- LOG.D (TAG, "Change directory:" +filename);
- Changedirectory (filename);
- }
- }
- }
- Mftpclient.changedirectory (path);
- Mftpclient.changedirectoryup (); Is the method that is ultimately called above.
- Then in the long press:
- Ftpfile Ftpfile=mfilelist.get (i);
- String Filename=ftpfile.getname ();
- if (".."). equals (filename) | | ".". Equals (filename) {
- LOG.D (TAG, "no treatment Here");
- } Else {
- If (Ftpfile.gettype () ==ftpfile.type_directory) {
- LOG.D (TAG, "Change directory:" +filename);
- Selectdirectory (filename);
- }
- }
- The Selectdirectory () method is to upload the destination as a selected directory. You can then pass in the local file to this directory.
- Called after changing the directory:
- Private void refreshfiles (ftpclient mftpclient) throws IOException, Ftpillegalreplyexception, ftpexception {
- Ftpfile[] files=new ftpfile[0];
- try {
- Files=mftpclient.list ();
- List<ftpfile> ftpfiles= (list<ftpfile>) arrays.aslist (files);
- Mfilelist.clear ();
- Mfilelist.addall (Ftpfiles);
- Message Message=message.obtain (Mhandler, refresh_file);
- Message.obj=mftpclient.currentdirectory ();
- Mhandler.sendmessage (message);
- Here is the refresh ListView.
- } catch (Ftpdatatransferexception e) {
- E.printstacktrace ();
- } catch (Ftpabortedexception e) {
- E.printstacktrace ();
- } catch (Ftplistparseexception e) {
- E.printstacktrace ();
- }
- }
- With the ability to upload and download, you can use it, build a simple FTP server, Homeftpserver is a good thing, and then take photos or videos to upload. The other will not say, directly to the code. The completed program can be used, but somewhat rudimentary, for informational purposes only. The current demand is to take photos to video and upload to FTP.
ftpj4:http://www.sauronsoftware.it/projects/ftp4j/download.php:ftp4j 1.7.1 release, Java FTP client class Library
Ftp4j 1.7.1 Released, this version of Ftpconnector new Setusesuggestedaddressfordataconnections () method is used to determine whether to trust the server's address returned via the PASV command, fixed automatic noop Timer bug, call SimpleDateFormat to thread installation mode, improve the data transfer program to be compatible with more servers and proxy servers.
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 1.7 was released, and the main improvements included:
- Modifications to the FTP connector schema
- Ftpconnector is no longer an interface, it becomes an abstract class, and some connections with timeout detection are implemented.
- Callers can set time-outs for connection, read, and shutdown operations
- Provide a method for interrupting the connection, ftpclient.abortcurrentconnectionattempt ();
- Fixed bug in Httptunnelconnector
http://www.sauronsoftware.it/projects/ftp4j/download.php
http://blog.csdn.net/dahuaishu2010_/article/details/7640768