Android FTP multi thread breakpoint continuous download \ upload instance

Recently, I added FTP breakpoint download and upload functions to my open source download framework aria. In this process, I climbed many holes in FTP and finally realized the function. Here, I record and download some core function points.

FTP download principle

FTP single thread breakpoint continuation

FTP is different from the traditional HTTP protocol. Because FTP does not have a so-called header file, we cannot specify the download interval to the server by setting the header like http.

However, the FTP protocol provides a better command rest for recovering tasks from the specified location. At the same time, the FTP protocol also provides a command size for obtaining the downloaded file size. With these two commands, there is no problem with FTP breakpoint continuation.

The principle of FTP breakpoint continuation is similar to that of HTTP breakpoint continuation. When pausing, record the stop position of the file. When downloading again, read the recorded position first. If the position exists, tell the server to download from the specified interval through the rest command.

FTP multi thread breakpoint continuation

The principle of multi-threaded download is similar to that of HTTP multi-threaded download. First obtain the file size, and then download the whole file in segments according to the number of threads. When the task stops, record the pause position of each thread and restart the download. Each thread reads the corresponding download record, and then each thread starts downloading from the specified position.

Segmented Download

Different from HTTP, FTP does not provide an API for file interval. Therefore, FTP has only a start position and no end position in segmented download. Therefore, you need to manually stop the thread at the specified location.

Function realization

This paper will use Apache commons net to realize FTP breakpoint continuous download \ upload function< br>

Through the following steps, you can easily realize FTP breakpoint continuation.

Sign in

FTP protocol is different from HTTP protocol. When downloading using FTP, you need to log in.

Of course, if your server has no login function, you can ignore the login operation.

Through the above three lines of code, you can easily log in to the FTP server.

After logging in, you also need to verify whether the login is successful

In the FTP protocol, there are multiple successful connection states, so ftprep.ispositivecomplete (reply) is required to verify whether the connection to the FTP server is successful.

File information acquisition

After connecting to the FTP server, you need to start obtaining the most important parameters (file length and file name) for downloading.

The client can obtain the file list of this path on the FTP server through client.listfiles (remotepath).

If your file name is in English and there is no Chinese in the path, you can get the correct file information through the above code.

However, if the file name on the FTP server has Chinese or the path has Chinese, you will not get the correct file information with the above code.

Correct writing

Since the default encoding of the FTP server is iso-8859-1, when the client obtains file information

Through the above code, you can get the correct file information.

File download

Configure the download interval of each thread

In the above code, there are mainly two steps:

FTP segmentation thread interval stops automatically

Because there is no interval download in FTP protocol, in order to let the thread download only the content of a specific interval, the client needs to stop the thread when the cumulative read data length of a single thread has exceeded the allocated interval length.

There are still several pits to deal with:

About FTP file upload

The method and principle of FTP file breakpoint continuation are similar to those of downloading:

The difference from downloading is:

Final effect

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>