Java multi-threaded file download

Source: Internet
Author: User
Tags throwable

The following code is a bit of a problem, there will be blocking, do not know what the problem:

 packagecom.test.service;Importjava.io.File;Importjava.io.InputStream;Importjava.io.RandomAccessFile;Importjava.net.HttpURLConnection;Importjava.net.URL;Importjava.util.concurrent.CountDownLatch;Importorg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.beans.factory.annotation.Value;Importorg.springframework.stereotype.Component;/*** <p> * multi-threaded file download, improve the download speed of large files (not used) * <p> **/@Component public classMulitthreaddownload {Private StaticLogger Logger = Loggerfactory.getlogger (mulitthreaddownload.class); @Value ("${onair.download.threadsize:5}")    Private intThreadsize = 5; @Value ("${onair.download.timeout:5000}")    Private intdownloadtimeout; Static BooleanFlag =true; //message    Private FinalCountdownlatch Msgdownlatch =NewCountdownlatch (1); //Worker Threads    Private FinalCountdownlatch Workdownlatch =NewCountdownlatch (threadsize); Privatedowloadrunnable[] Dowloadrunnables =Newdowloadrunnable[threadsize];  public Static voidmain (string[] Args) {NewMulitthreaddownload (). downloadFile ("", "g:\\123.mp4"); }         public BooleandownloadFile (String url,string filePath) {logger.debug (": {}, destination file path: {}", url,filepath); Try{URL URLPath=NewURL (url); HttpURLConnection Conn=(httpurlconnection) urlpath.openconnection ();            Conn.setconnecttimeout (downloadtimeout); Conn.setrequestmethod ("GET"); intStatus =Conn.getresponsecode (); if(status = = 200) {//200 return all, 206 return part//file Length                intLength =Conn.getcontentlength (); Logger.info ("get file Size: {}", length); //Create download file specify sizeRandomaccessfile RAF =NewRandomaccessfile (NewFile (filePath), "rwd");                Raf.setlength (length); Raf.close (); //Freeing Resources//chunking size                intBlockSize = length/threadsize; //Create worker Threads                 for(inti = 1; I <= threadsize; i++) {                    intStartIndex = blocksize* (i-1); intEndIndex = blockSize * i-1; if(i = =Threadsize) {endIndex=length; } logger.info ("thread: {} Download file start point: {} end Point: {}", i,startindex,endindex); Dowloadrunnables[i-1] =Newdowloadrunnable (url,filepath,msgdownlatch, workdownlatch, i,startindex,endindex); Thread Thread=NewThread (dowloadrunnables[i-1]);                    Thread.Start (); Thread.setuncaughtexceptionhandler (NewThread.uncaughtexceptionhandler () {@Override public voiduncaughtexception (Thread t, throwable e) {logger.debug ("catch to exception", e); Flag=false;                                    }                    }); }                //notify worker thread to start, start workingMsgdownlatch.countdown (); Logger.debug ("main thread blocked, waiting for worker threads to complete task"); //start a thread to monitor the download progress//Moniterlength (length); //block the main thread, waiting for the worker thread to finishworkdownlatch.await (); Logger.debug ("worker threads complete task, main thread continues"); returnflag; }                    } Catch(throwable e) {logger.error ("file Download failed:" +e.getmessage (), e); File File=NewFile (filePath); if(file.exists ()) {file.delete ();//Download failed to delete temp file            }        }        return false; }    //Output Download Progress    Private voidMoniterlength (intLength) {        NewThread (NewRunnable () {@Override public voidRun () { while(getdownloadlength () <Length) {logger.debug ("file size: {}, Current Download size: {}, progress {}", length,getdownloadlength (), getdownloadlength () * 1.0/(Long) length); Try{thread.sleep (10000); } Catch(interruptedexception E) {//TODO auto-generated Catch blockE.printstacktrace ();    }}}). Start (); }    //Monitor Download Progress     public intgetdownloadlength () {intLength = 0;  for(inti = 0; I < dowloadrunnables.length; i++) {length+=dowloadrunnables[i].downloadlength; }        returnlength; }    }//Download ThreadclassDowloadrunnableImplementsrunnable{Private StaticLogger Logger = Loggerfactory.getlogger (dowloadrunnable.class); PrivateCountdownlatch msgdownlatch; PrivateCountdownlatch workdownlatch; Private intthreadindex; Private intstartIndex; Private intendIndex; PrivateString url; PrivateString filePath;  public intdownloadlength;//Downloaded size         publicdowloadrunnable (string url, string filePath, countdownlatch msgdownlatch, countdownlatch workdownlatch, intthreadindex,intstartIndex,intEndIndex) {         this. url =url;  this. FilePath =filePath;  this. Msgdownlatch =msgdownlatch;  this. Workdownlatch =workdownlatch;  this. Threadindex =threadindex;  this. StartIndex =startIndex;  this. EndIndex =endIndex; } @Override public voidRun () {Try {                //block this thread, waiting for the main thread to give the startup message (msgdownlatch.countdown ());msgdownlatch.await (); //Specific WorkLogger.info ("thread {} Task start", threadindex); URL URLPath=NewURL (url); HttpURLConnection Conn=(httpurlconnection) urlpath.openconnection (); Conn.setconnecttimeout (5000); Conn.setrequestproperty ("Range", "bytes=" + startIndex + "-" +endIndex); Conn.setrequestmethod ("GET"); intStatus =Conn.getresponsecode (); Logger.debug ("thread {} Request returned responsecode:{}", threadindex,status); if(status==206) {inputstream in=Conn.getinputstream (); Randomaccessfile RAF=NewRandomaccessfile (filePath, "rwd");                                        Raf.seek (startIndex); byte[] buffer =New byte[2048]; intLength = 0; Logger.debug ("thread {} start writing data, start point {}", threadindex,startindex);  while(length = In.read (buffer))! =-1){                        //logger.debug ("thread {} read size: {}", threadindex,length);Raf.write (buffer, 0, length); //Downloadlength + = length;} raf.close ();                In.close (); }Else{logger.error ("file Download failed, Status code:" +status); Throw NewException ("file Download failed, status code:" +status); } logger.info ("thread {} Task complete", threadindex); //work doneWorkdownlatch.countdown (); } Catch(throwable e) {logger.error (e.getmessage (), e);            E.printstacktrace (); }    }}
Mulitthreaddownload.java

Don't see what the problem, first write it down!

Java multi-threaded file 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.