Simple multi-thread download using Java

Source: Internet
Author: User

Today, I wrote a simple multi-thread HTTP download program in Java, mainly using the HTTP protocol and the randomaccessfile class. It is easy to add the resumable upload function, so that you can implement functions similar to "Little Network ant.

Public class downloadnettest {
Private file fileout;
Private URL;
Private long filelength = 0;
// Number of initialization threads
Private int threadnum = 5;

Public downloadnettest (){
Try {
System. Out. println ("link URL ");
Url = new URL ("http: // 211.64.201.201/uploadfile/nyz ");
Httpurlconnection urlcon = (httpurlconnection) URL. openconnection ();
Filelength = urlcon. getcontentlength ();
If (urlcon. getresponsecode ()> = 400 ){
System. Out. println ("server response error ");
System. Exit (-1 );
}
If (filelength <= 0)
System. Out. println ("unable to know the file size ");
// Print information
Printmime (urlcon );
System. Out. println ("file size:" + filelength/1024 + "K ");
// Get the file name
String trueurl = urlcon. geturl (). tostring ();
String filename = trueurl. substring (trueurl. lastindexof ('/') + 1 );
Fileout = new file ("D: //", filename );
}
Catch (malformedurlexception e ){
System. Err. println (E );
}
Catch (ioexception e ){
System. Err. println (E );
}
Init ();
}
Private void Init (){
Downloadnetthread [] Down = new downloadnetthread [threadnum];
Try {
For (INT I = 0; I <threadnum; I ++ ){
Randomaccessfile randout = new randomaccessfile (fileout, "RW ");
Randout. setlength (filelength );
Long block = filelength/threadnum + 1;
Randout. Seek (Block * I );
Down [I] = new downloadnetthread (URL, randout, block, I + 1 );
Down [I]. setpriority (7 );
Down [I]. Start ();
}
// Cyclically determine whether the download is complete
Boolean flag = true;
While (FLAG ){
Thread. Sleep (500 );
Flag = false;
For (INT I = 0; I <threadnum; I ++)
If (! Down [I]. isfinished ()){
Flag = true;
Break;
}
} // End while
System. Out. println ("the file has been downloaded and saved in" + fileout. getpath ());
} Catch (filenotfoundexception e ){
System. Err. println (E );
E. printstacktrace ();
}
Catch (ioexception e ){
System. Err. println (E );
E. printstacktrace ();
}
Catch (interruptedexception e ){
System. Err. println (E );
}

}
Private void printmime (httpurlconnection HTTP ){
For (INT I = 0; I ++ ){
String mine = http. getheaderfield (I );
If (Mine = NULL)
Return;
System. Out. println (HTTP. getheaderfieldkey (I) + ":" + mine );
}
}

Public static void main (string [] ARGs ){
Downloadnettest APP = new downloadnettest ();
}

}


// Thread class
Public class downloadnetthread extends thread {
Private inputstream Randin;
Private randomaccessfile randout;
Private URL;
Private long block;
Private int threadid =-1;
Private Boolean done = false;

Public downloadnetthread (URL, randomaccessfile out, long block, int threadid ){
This. url = URL;
This. randout = out;
This. Block = block;
This. threadid = threadid;
}
Public void run (){
Try {
Httpurlconnection HTTP = (httpurlconnection) URL. openconnection ();
HTTP. setrequestproperty ("range", "bytes =" + block * (threadId-1) + "-");
Randin = http. getinputstream ();
}
Catch (ioexception e ){
System. Err. println (E );
}

////////////////////////
Byte [] buffer = new byte [1024];
Int offset = 0;
Long localsize = 0;
System. Out. println ("Thread" + threadid + "Start download ");
Try {
While (offset = Randin. Read (buffer ))! =-1 & localsize <= block ){
Randout. Write (buffer, 0, offset );
Localsize + = offset;
}
Randout. Close ();
Randin. Close ();
Done = true;
System. Out. println ("Thread" + threadid + "download completed ");
This. Interrupt ();
}
Catch (exception e ){
System. Err. println (E );
}
}
Public Boolean isfinished (){
Return done;
}
}

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.