java--using multi-threaded download, breakpoint Continuation Technology principle (Randomaccessfile)

Source: Internet
Author: User
Tags file transfer protocol

I. Basic knowledge 1. What is a thread? What is a process? What is the relationship between them?

You can refer to the previous article: Java Core Knowledge Point learning----concurrency and parallelism differences, process and thread differences, how to create threads and threads in four states, what is thread timers

Simply say a process can consist of multiple threads, one operating system can be multiple processes, and they can all work at the same time.

2. What is download? How do I download multiple threads? How do I resume a breakpoint?

Broadly speaking, what you see on the screen that is not part of the local computer is "downloaded" . In the narrow sense, people only think that the local disk storage location where the downloaded files are customized is the "download";.

Web download is divided into two types of HTTP and FTP, respectively, Hyper Text transportation Protocol (Hypertext Transfer Protocol) and file transportation Protocol (File Transfer Protocol) abbreviation, They are the means of exchanging data between computers, is also the two most classic download method, the principle of the download is very simple, that is, the user two rules (protocol) and the server to provide the file to contact and move the files to their own computer, so as to achieve the download function.
Multi-threaded download, that is, a file can be more than one thread to download, and the breakpoint continues to legend is when a file download to half suddenly because of some reason download interrupted , such as suddenly the computer shutdown, then when the boot has been downloaded to half of the files do not need to start, but then download; The principle is simple : First, the download interrupted when the last point of time to remember the location of the download, and then continue to download the location , the continuation of the download can be manually triggered by the program can also be automatically recognized during the operation of the download.

3. What is Randomaccessfile?

The only parent class for Randomaccessfile is object, which is different from other stream parent classes. is used to access files that hold data records, so you can use the Seek () method to access the records and read and write them. The sizes of these records do not have to be the same, but their size and position must be knowable.

The way Randomaccessfile works is to glue DataInputStream and dataoutputstream together, plus some of its own methods, such as the positioning of the Getfilepointer (), the move in the file Seek (), and determine the length of the file size (). In addition, its constructor has a parameter that indicates whether to open the file as read-only ("R") or read-write ("RW") (fopen () of C). It does not support write-only files, from this point of view, if Randomaccessfile inherit the DataInputStream, it may do better.

So, in this case, we use Randomaccessfile's seek to remember the last access record, and then the last access was downloaded.

randomaccess literal translation comes from random access , so that the understanding is easy to cause trouble, that is, random, then how to control the progress?

A hint of the word by random:

' Rænd?m] Adj. random, arbitrary , random, randomly selected, arbitrarily random, arbitrarily arbitrary, random, arbitrary , Random, casual, (words, etc.) the mouth of the mouth, (people, etc.) accidentally encountered, random choice, casual, insignificant, casual

You can see that it has any meaning, which means that you can access the file anywhere, so that you can explain why the breakpoint continues to pass.

Two. Program implementation

Here is an example of a tomcat download: http://tomcat.apache.org/download-70.cgi#7.0.54 (Tomcat is currently the latest version is 7.0.54,2014-07-02)

F12 Open Chrome's Webdeveloper network window and click Download, as shown in:

It is important to note that the contents of the request headers, such as Requestmethod,accept,accept-language,connection , ETC., You need to take these things with you when you make the request. Download the Tomcat 7.0.54 download link here: http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.54/bin/ Apache-tomcat-7.0.54.zip

implementation code: Downutil.java
Package com.amos.tool;import java.io.inputstream;import java.io.randomaccessfile;import  Java.net.httpurlconnection;import java.net.url;/** * Created by Amosli on 14-7-2.     */

Public classdownutil{//define the path to the download resource PrivateString Path; //specify where to save the downloaded file PrivateString targetfile; //define how many threads need to be used to download resources Private intThreadnum; //defining the downloaded thread object Privatedownthread[] Threads; //define the total size of downloaded files Private intfileSize; PublicDownutil (string path, String targetfile,intthreadnum) { This. Path =path; This. Threadnum =Threadnum; //initializing the Threads arrayThreads =NewDownthread[threadnum]; This. TargetFile =targetfile; } Public voidDownload ()throwsException {URL url=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setconnecttimeout (5 * 1000); Conn.setrequestmethod ("GET"); Conn.setrequestproperty ("Accept", "Image/gif, Image/jpeg, Image/pjpeg, Image/pjpeg," + "Application/x-shockwave-flash, application/x Aml+xml, "+" application/vnd.ms-xpsdocument, application/x-ms-xbap, "+" AP Plication/x-ms-application, Application/vnd.ms-excel, "+" Application/vnd.ms-powerpoint, Applicati On/msword, */* "); Conn.setrequestproperty ("Accept-language", "ZH-CN"); Conn.setrequestproperty ("Charset", "UTF-8"); Conn.setrequestproperty ("Connection", "keep-alive"); //Get File SizeFileSize =conn.getcontentlength (); Conn.disconnect (); intCurrentpartsize = filesize/threadnum + 1;//There is no need to add 1, no add 1 can also randomaccessfile file=NewRandomaccessfile (TargetFile, "RW"); //to set the size of a local filefile.setlength (fileSize); File.close (); for(inti = 0; i < threadnum; i++) { //calculate the start location of each thread's download intstartpos = i *currentpartsize; //each thread is downloaded using a randomaccessfileRandomaccessfile Currentpart =Newrandomaccessfile (TargetFile,"RW"); //Locate the download location for this threadCurrentpart.seek (startpos); //Create a download threadThreads[i] =NewDownthread (Startpos, Currentpartsize, Currentpart); //start the download threadThreads[i].start (); } } //get percent complete of downloads Public Doublegetcompleterate () {//count the total size of multiple threads that have been downloaded intSumsize = 0; for(inti = 0; i < threadnum; i++) {sumsize+=threads[i].length; } //returns the percentage that has been completed returnSumsize * 1.0/fileSize; } Private classDownthreadextendsThread {//download location for current thread Private intstartpos; //defines the file size that the current thread is responsible for downloading Private intcurrentpartsize; //the file block that the current thread needs to download PrivateRandomaccessfile Currentpart; //defines the number of bytes already downloaded by the thread Public intlength; PublicDownthread (intStartpos,intcurrentpartsize,randomaccessfile Currentpart) { This. startpos =startpos; This. currentpartsize =currentpartsize; This. Currentpart =Currentpart; } @Override Public voidrun () {Try{URL URL=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setconnecttimeout (5 * 1000); Conn.setrequestmethod ("GET"); Conn.setrequestproperty ("Accept", "Image/gif, Image/jpeg, Image/pjpeg, Image/pjpeg," + "Application/x-shockwave-flash, appli Cation/xaml+xml, "+" application/vnd.ms-xpsdocument, APPLICATION/X-MS-XBAP, " + "application/x-ms-application, application/vnd.ms-excel," + "Applicat Ion/vnd.ms-powerpoint, Application/msword, */* "); Conn.setrequestproperty ("Accept-language", "ZH-CN"); Conn.setrequestproperty ("Charset", "UTF-8"); InputStream instream=Conn.getinputstream (); //skips startpos bytes, indicating that the thread only downloads which part of the file it is responsible for. Instream.skip ( This. startpos); byte[] buffer =New byte[1024]; intHasread = 0; //read network data and write to local file while(Length <currentpartsize&& (Hasread = instream.read (buffer))! =-1) {currentpart.write (buffer,0, Hasread); //Cumulative total size of this thread downloadLength + =Hasread; } currentpart.close (); Instream.close (); } Catch(Exception e) {e.printstacktrace (); } } }}

Test: Downutiltest.java
 PackageCom.amos;ImportCom.amos.tool.DownUtil;ImportOrg.omg.PortableServer.THREAD_POLICY_ID;/*** Created by Amosli on 14-7-2.*/ Public classDownutiltest { Public Static voidMain (String args[])throwsException {FinalDownutil Downutil =NewDownutil ("Http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.54/bin/apache-tomcat-7.0.54.zip", " Tomcat-7.0.54.zip ", 3);        Downutil.download (); NewThread (NewRunnable () {@Override Public voidrun () { while(Downutil.getcompleterate () <1) {System.out.println ("Completed:" +downutil.getcompleterate ()); Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace ();    }}}). Start (); }}

Results:

Download my avatar:

  Final New Com.amos.DownUtil ("Http://pic.cnitblog.com/avatar/534352/20131215160918.png", "Amosli.png", 2);

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.