The principle of multi-threaded accelerated download

Source: Internet
Author: User

multi-threaded download steps:

1. Be aware of the size of the server-side resources.
        
The server URL is fetched through the URLConnection request.
urlconnection.getcontentlength ();//The size of the resource
            
2. Create a local file (placeholder) of the same size as the server resource
//file: file; mode: File pattern, RWD: write directly to the underlying device, HDD
randomaccessfile randomfile =new randomaccessfile (File file,string mode)
                    
randomfile.setlength (long size);//Create a file as large as the server resource
                
3. To assign the starting and ending locations of each thread's download file.
                
4. Turn on the thread to perform the download
download some resources through URLConnection.
Note:
1. Need Range header, Key:range value:bytes:0-499
urlconnection.setrequestpropety ("Range", "bytes:0-499")
2. You need to set the start location for each thread where the local file is saved
randomaccessfile randomfile =new randomaccessfile (File file,string mode)
randomfile.seek (int startpostion);//The start location where the thread download is saved.
                        
5. Be aware that each thread has been downloaded.

The principle of multi-threaded accelerated 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.