Java Multithreaded Download Example

Source: Internet
Author: User


Project structure:














Filedownload.java:

Package Com.wl.download;import Java.io.file;import Java.io.filenotfoundexception;import java.io.IOException;import Java.io.randomaccessfile;import Java.text.decimalformat;import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Com.wl.util.downloadthread;import com.wl.util.HttpClientFactory; public class FileDownload {public static void main (string[] args) {Long startTime = System.currenttimemillis (); System.out.println (StartTime); String Srcfileurl = "Http://172.16.2.40:80/2014/12/gpfqpfvlwsvka2mj39pn.mprx"; String Destfilepath = "g:/download/destfile.mprx";//Gets the file size long fileSize = Getremotefilesize (srcfileurl);/** * File size to download per thread (self-tuning) *///long unitsize = 1024;  Multithreading too much stuck your computer no problem long unitsize = 1024 * 1024 * 2; Download file size = 535.66M;  23559 Millisecond//long unitsize = 1024 * 1024 * 50; Download file size = 535.66M;  35726 Millisecond//long unitsize = fileSize; Download filesize = 535.66M; Time used = 52865 millisecond//Create destination file and specify size CreateFile (Destfilepath, fileSize);//Open several threads to download long threadcount = fileSize/ unitsize;//Write file location long offset = 0;if (fileSize <= unitsize) {//If the remote file size is less than or equal to unitsizedownloadthread downloadthread = NE W Downloadthread (Srcfileurl, Destfilepath, offset, fileSize);  Thread t = new thread (downloadthread); t.setpriority (ten); T.start (); try {t.join (); The method functions: The main thread (here is the threads that the main function executes) waits for the download thread to end} catch (Interruptedexception e) {}}else{for (int i = 1; I <= threadcount; i++) { Downloadthread downloadthread = new Downloadthread (Srcfileurl, Destfilepath, offset, unitsize); Thread t = new thread (downloadthread); t.setpriority (ten); T.start (); offset = offset + unitsize;} if (fileSize% unitsize! = 0) {//If not divisible, you need to create another thread to download the remaining bytes Downloadthread downloadthread = new Downloadthread (Srcfileurl, DE Stfilepath, offset, filesize-unitsize * threadcount); Thread t = new thread (downloadthread); t.setpriority (ten); T.start (); try {t.join ();///The method acts: the main thread (here is the one that the main function executes) waits for all The main thread produces the end of the download thread} catch (Interruptedexception e) {}}}long endTime = System.currenttimemillis (); System.out.println (EndTime);  System.out.println ("Download File size =" + filesize2string (fileSize) + "; Time used = "+ (endtime-starttime) +" millisecond ");} /** * Get file size */private static long getremotefilesize (String srcfileurl) {HttpClient HttpClient = Httpclientfactory.getins Tance (). Gethttpclient (); HttpGet httpget = new HttpGet (srcfileurl);//Send request, return response HttpResponse response = null;try {response = Httpclient.execute (htt Pget);} catch (Clientprotocolexception e) {} catch (IOException e) {}long result = Response.getentity (). Getcontentlength (); return result;}  /** * Create a file of the specified size * @param fileName * @param fileSize */private static void CreateFile (String fileName, long fileSize) {file NewFile = new File (fileName), if (Newfile.exists ()) Newfile.delete (); Randomaccessfile RAF = null;try {RAF = new Randomaccessfile (newFile, "RW"); Raf.setlength (fileSize);} catch ( FileNotFoundException e) {} catch (IOException e) {} finally {try {if (RAF! = null) {Raf.close ()}} catch (IOException e) {}}}/** * Get size String * @param totalsize * @return */private S        Tatic string filesize2string (Long totalsize) {string filetotalsize= "";    DecimalFormat df = new DecimalFormat ("0.00");    if (TotalSize <1024) {filetotalsize=totalsize+ "B";    }else if (totalsize <1048576) {Filetotalsize=df.format ((float) totalsize/1024) + "K";    }else if (totalsize <1073741824) {Filetotalsize=df.format (float) totalsize/1048576) + "M";    }else{Filetotalsize=df.format ((float) totalsize/1073741824) + "G"; } return filetotalsize;}}

Downloadthread.java

Package Com.wl.util;import Java.io.bufferedinputstream;import Java.io.ioexception;import Java.net.httpurlconnection;import Java.net.url;public class Downloadthread implements Runnable {private String URL = null;//file to download private String file = null;//Local storage path private long offset = 0;//offset private Long length = 0;//number of bytes allocated to this thread pub Lic downloadthread (string url, string file, long offset, long length) {This.url = Url;this.file = File;this.offset = OFFSE t;this.length = length;} public void Run () {Bufferedinputstream bis = null; Saveitemfile Saveitemfile = null;try {httpurlconnection httpurlconnection = (httpurlconnection) New URL ( This.url.replace ("", "%20")). OpenConnection (); Httpurlconnection.setrequestmethod ("GET"); Httpurlconnection.setrequestproperty ("RANGE", "bytes=" + This.offset + "-" + (This.offset + this.length-1)); bis = new Buf Feredinputstream (Httpurlconnection.getinputstream ()); saveitemfile = new Saveitemfile (file,offset); byte[] Buff = new Byte[1024*8];while (length = bis.read (Buff)) > 0 && Offset < offset+length) {//write to file contents, return the last written length offset + = saveitemfile.write (buff, 0, (i            NT) length); }} catch (IOException e) {} finally {try {if (bis! = null) {Bis.close ()} if (saveitemfile!=null) {saveitemfile.close ();}} catch (IOException e) {}}}}

Httpclientfactory.java:

Package Com.wl.util;import Java.util.concurrent.timeunit;import Org.apache.http.httpversion;import Org.apache.http.client.httpclient;import Org.apache.http.conn.scheme.plainsocketfactory;import Org.apache.http.conn.scheme.scheme;import Org.apache.http.conn.scheme.schemeregistry;import Org.apache.http.conn.ssl.sslsocketfactory;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.impl.conn.poolingclientconnectionmanager;import Org.apache.http.params.basichttpparams;import Org.apache.http.params.coreconnectionpnames;import Org.apache.http.params.httpparams;import Org.apache.http.params.httpprotocolparams;public class Httpclientfactory {private HttpClient HttpClient = null;/** * A unique instance of this class may exist */private static httpclientfactory m_instance;/** * Static Factory method * * @return return a single instance of the Readconfigation class */public s Tatic httpclientfactory getinstance () {if (null = = m_instance) {m_instance = new httpclientfactory ();} return m_instance;} Private Httpclientfactory () {//Set component parameters, version of HTTP protocol,1.1/1.0/0.9httpparams params = new Basichttpparams (); Httpprotocolparams.setversion (params, httpversion.http_1_1); Httpprotocolparams.setuseragent (params, "httpcomponents/1.1"); Httpprotocolparams.setuseexpectcontinue (params, true);//Set connection Timeout int request_timeout = 30 * 1000; Set request Timeout 10 seconds int so_timeout = 10 * 1000; Set wait data timeout time 10 seconds//Httpconnectionparams.setconnectiontimeout (params, request_timeout);// Httpconnectionparams.setsotimeout (params, so_timeout);p Arams.setparameter (coreconnectionpnames.connection_ Timeout,request_timeout);p arams.setparameter (Coreconnectionpnames.so_timeout, so_timeout);// Set the Access protocol Schemeregistry Schreg = new Schemeregistry (); Schreg.register ("http", 80, Plainsocketfactory.getsocketfactory ())); Schreg.register ("https", 443, Sslsocketfactory.getsocketfactory ()));//multi-connected thread-Safe manager poolingclientconnectionmanager PCCM = new Poolingclientconnectionmanager (Schreg);p Ccm.setdefaultmaxperroute (50); Maximum number of parallel links per host pccm.setmaxtotal (100); Client total parallel link Maximum number HttpclieNT = new Defaulthttpclient (PCCM, params); Httpclient.getconnectionmanager (). Closeidleconnections (10, Timeunit.seconds);} Public Defaulthttpclient gethttpclient () {return (defaulthttpclient) httpClient;}}

Saveitemfile.java:

Package Com.wl.util;import Java.io.ioexception;import Java.io.randomaccessfile;public class SaveItemFile {//Store file PR        Ivate Randomaccessfile Itemfile;    Public Saveitemfile () throws IOException {This ("", 0); }/** * @param name file path, name * @param POS Write point location position * @throws IOException */Public SaveItem File (String name, long Pos) throws IOException {itemfile = new Randomaccessfile (name, "RW");//readable writable//in the specified PO    The s position begins writing data Itemfile.seek (POS); }/** * <b>function:</b> Sync method Write file * @author Hoojo * @createDate 2011-9-26 12:21:22 * @param buff Buffer array * @param start position * @param length * @return */public int write (byte[] buff, int        Start, int length) {int i =-1;            try {itemfile.write (buff, start, length);        i = length;        } catch (IOException e) {e.printstacktrace ();    } return i; } public void Close() throws IOException {if (itemfile! = null) {itemfile.close (); }    }}

Pom.xml

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >    < modelversion>4.0.0</modelversion>  <groupId>com.filedownload</groupId>  < artifactid>filedownload</artifactid>  <version>0.0.1-SNAPSHOT</version>    < dependencies>    <dependency>         <groupId>org.apache.httpcomponents</groupId>         < Artifactid>com.springsource.org.apache.httpcomponents.httpclient</artifactid>         <version> 4.2.1</version>     </dependency>    </dependencies></project>

Over

Multi-threaded upload to be continued .....

Java Multithreaded Download Example

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.