Use Apache Commons Net API to implement the pit point during FTP upload and download

Source: Internet
Author: User

Recent projects need to implement FTP upload, download function, using the Apache Commons Net API. The code is done quickly, but because the API usage scenario is not very familiar, take a few detours, and take a moment to summarize.

A)Active Passive Mode selection:FTPa detailed introduction to active and passive modes is available for reference (http://blog.csdn.net/huanggang028/article/details/41248663). The idea is that the active mode is the client sending it to the server .PORTcommand, and then the service side passes -The data port connects to the client-enabled port and sends the data, and the passive mode is the client sending it to the serverPASVcommand, the server randomly opens a port and notifies the client that the client establishes a connection to the server based on that port and then sends the data. The server is two modes, which depends on the client, and the key point is that the network environment is suitable for which mode, such as the client inside the firewall, it is best to choose Passive mode. Ftpclient.enterlocalactivemode ()is to configure the active mode, andFtpclient.enterlocalpassivemode ()is configured as passive mode.

B) file format selection: The default is that the type is ASCII(plain text file), but in order to make support for any type, it is recommended to set to Binary_file_type, And it is called setfiletype (int fileType)before the upload and download operations begin.

C)Upload, download the use of overloaded methods of choice: such as uploading,boolean storefile (String remote, InputStream local)and theoutputstream storefilestream (String remote), the difference is the first method if we don't care exactly what has been fromInputStreamHow many bytes are passed in, fully relying on the internal implementation of the interface to complete the input and output operations, and the second method allows us to control the transfer of the speed of the byte during the upload of additional operations, such as the implementation of the transfer progress can rely on this method. However, it should be noted that if the second method is used, we must rely on us to achieve whether the upload completed, downloaded after the post-control, otherwise there will be problems. For example, we use the second method, we need to close the flow manually, and immediately after the callCompletependingcommand ()method. If the first method cannot be used to invoke aCompletependingcommand ()method, because once it is called, it waitsFTPServer returns226Transfer Complete, butFTPthe server is executed only on the receiving streamClosemethod is not returned. Should not be called when the call will appear zombie phenomenon, and error:

<span style= "FONT-SIZE:18PX;" >    14/11/19 13:43:29 ERROR expand. FTPSUPPORT:FTP response 421 received.  Server closed Connection.org.apache.commons.net.ftp.FTPConnectionClosedException:FTP Response 421 received.  Server closed connection.at org.apache.commons.net.ftp.ftp.__getreply (ftp.java:363) at Org.apache.commons.net.ftp.ftp.__getreply (ftp.java:290) at org.apache.commons.net.ftp.FTP.getReply (ftp.java:637) At Org.apache.commons.net.ftp.FTPClient.completePendingCommand (ftpclient.java:1637) </span>


Use Apache Commons Net API to implement the pit point during FTP upload and download

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.