Java Multithreading Download Instance detailed _java

Source: Internet
Author: User
Tags int size

This example describes Java multithreaded downloads. Share to everyone for your reference, specific as follows:

The use of multi-threaded download files can be faster to complete the download of files, multithreaded download file is fast because of its preempted server resources. such as: Assuming that the server serves up to 100 users at the same time, one thread in the server corresponds to one user, 100 threads are not executed concurrently in the computer, but the CPU divides the time slice to perform alternately, if a application uses 99 threads to download the file, then it occupies 99 user resources. Suppose the average CPU execution time allocated to each thread in one second is that the 10ms,a application gets 990ms of execution time in one second of the server, while the other applies only 10ms execution time in a second. Just like a faucet, where the amount of water per second is equal, it must be more than 10 milliseconds to put 990 milliseconds of water.

Multi-threaded Download implementation process:

1. First get the length of the download file, and then set the length of the local file.

Httpurlconnection.getcontentlength ();
Randomaccessfile file = new Randomaccessfile ("Youdao.exe", "RW");
File.setlength (filesize)//Set the length of the local file

2. Calculate the data length and download location of each thread download based on file length and number of threads. For example, the length of the file is 6M and the number of threads is 3, so each thread downloads a data length of 2M, where each thread starts downloading as shown in the following figure.

3. Use the HTTP Range header field to specify where each thread starts downloading from the file, such as: Specifies that the file be downloaded from the 2M location of the file, as follows:

Copy Code code as follows:
Httpurlconnection.setrequestproperty ("Range", "bytes=2097152-");

4. Save the file and use the Randomaccessfile class to specify where each thread starts writing data from a local file

Randomaccessfile threadfile = new Randomaccessfile ("<span style=" font-family:arial, Helvetica, Sans-serif; " >youdao.exe</span><span style= "font-family:arial, Helvetica, Sans-serif;" > "RW");</span>
Threadfile.seek (2097152);//write data from where the file is located

The following is a specific implementation class:

Before writing the implementation class, we first put the downloaded file on the server and deploy:

I was placed here in the D:\Tomcat\apache-tomcat-7.0.37\webapps\doudou directory, and started D:\Tomcat\apache-tomcat-7.0.37\bin under the Startup.bat

1.downloadtest.java

Package Www.csdn.net.down;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.RandomAccessFile;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
 public class Downloadtest {public file file;
 Public Randomaccessfile Accessfile;
 Number of threads public static int threadnum = 3;
 Each thread is responsible for downloading the size int blockSize;
 Create access path public String path = "Http://localhost:8080/doudou/youdao.exe";
   public static int threadcount;//quantity public void Testdown () {try {//Create URL object url url = new URL (path);
   Creates a HttpURLConnection object httpurlconnection httpurlconnection = (httpurlconnection) URL. OpenConnection ();
   Set the way the send request is sent Httpurlconnection.setrequestmethod ("get");
   Sets whether the request is timed out httpurlconnection.setconnecttimeout (5000); Set HttpURLConnection. Setrequestproperty ("User-agent", "mozilla/5.0" (compatible; MsIE 10.0; Windows NT 6.2;
   trident/6.0) "); Whether to respond to success if (httpurlconnection.getresponsecode () = = 200) {//Get the size of the file int size = Httpurlconnection.getcontentl
    Ength ();
    SYSTEM.OUT.PRINTLN ("Size of file" + sizes);
    Create file = new filename ("Youdao.exe");
    Accessfile = new Randomaccessfile (file, "RWD");
    Sets the size of the file (accessfile.setlength);
    The size blockSize = size/threadnum per thread download; Open three threads to manipulate this file for (int i = 1; I <= threadnum i++) {//1 2 3//compute the position of the start of each thread int startsize = (I-
     1) * blockSize;
     End position int endsize = (i) * blockSize;  If the thread is the last thread if (i = = threadnum) {//Determine whether the file size is greater than the computed end position if (Size > Endsize) {//End position equals
      The size of the file is endsize = size;
     Create a random read for each thread randomaccessfile threadaccessfile = new Randomaccessfile (file, "RWD");
    New Thread (New Downloadthread (I, Threadaccessfile, startsize, endsize, Path)). Start (); }}} CATCH (malformedurlexception e) {//TODO auto-generated catch block E.printstacktrace ();
  catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
  } public static void Main (string[] args) {downloadtest downloadtest = new Downloadtest ();
 Invoke Download Method Downloadtest.testdown (); } class Downloadthread implements Runnable {//download files are encapsulated public randomaccessfile accessfile;//Each thread has a accessfile file
 Object thread 1 Thread 2 thread 3/thread download the starting position of the file public int startsize;
 public int endsize;
 The path path to the file download is public String path; public int threadId;
  Identification of threads public downloadthread (int threadId, randomaccessfile accessfile, int startsize, int endsize, String path) {
  This.threadid = threadId;
  This.accessfile = Accessfile;
  This.startsize = startsize;
  This.endsize = endsize;
 This.path = path;
   @Override public void Run () {//Execute the Run method try {//create file Threadfile = new ThreadId + ". txt");
   if (threadfile.exists ()) {//Read the contents of the file The input stream object that created the file FileInputStream fis = new FileInputStream (threadfile);
    Using the tool class to read byte data[] = Streamtools.istodata (FIS);
    Convert to String threadlen = new string (data); if (Threadlen!= null) && (! "".
     Equals (Threadlen))) {startsize = integer.valueof (Threadlen);
     Fix 416bug Error if (Startsize > endsize) {startsize = endSize-1;
   ()}///Create URL object url url = new URL (path);
   Creates a HttpURLConnection object httpurlconnection httpurlconnection = (httpurlconnection) URL. OpenConnection ();
   Sets the requested header Httpurlconnection.setrequestmethod ("get");
   Sets whether the request is timed out httpurlconnection.setconnecttimeout (5000); Set HttpURLConnection. Setrequestproperty ("User-agent", "mozilla/5.0" (compatible; MSIE 10.0; Windows NT 6.2;
   trident/6.0) ");
   Key Settings Httpurlconnection.setrequestproperty ("Range", "bytes=" + startsize + "-" + endsize); Output Current thread System.out.println ("current thread + threadId +" Download start location: "+ startsize +" Download End Position: "+ endsize";
   Response Success//Set the starting position of the random read file Accessfile.seek (startsize);
   Gets the corresponding stream object InputStream is = Httpurlconnection.getinputstream ();
   Create output stream object byte buffer[] = new byte[1024];
   int len = 0;
    int threadtotal = 0;//Save Record/while (len = is.read (buffer)!=-1) {accessfile.write (buffer, 0, len) after each thread is downloaded;
    Threadtotal + = len;//records the length of your written//xml file//The length of the download by file record file FileOutputStream fos = new FileOutputStream (threadfile);
    Fos.write ((Threadtotal + ""). GetBytes ());
    Fos.flush ();
   Fos.close ();
   } accessfile.close ();
   Is.close ();
   System.out.println (threadId + "thread execution complete");
    Thread operation synchronized (Downloadtest.class) {downloadtest.threadcount++; if (Downloadtest.threadcount >= downloadtest.threadnum) {for (int i=1;i<=downloadtest.threadnum;i++) {Fil
      E file = new file (i+ ". txt");
      if (file.exists ()) {file.delete (); catch (Malformedurlexception e) {//TODO auto-generated catch block E.printstacktrace ();
  catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();

 }
 }
}

2. Package Streamtools.java for streaming tools

Package Www.csdn.net.down;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
public class Streamtools {public
 static byte[] Istodata (InputStream is) throws ioexception{
  /byte output stream
  Bytearrayoutputstream Bops = new Bytearrayoutputstream ();
  Cache
  byte buffer[] = new byte[1024] = read data;
  Read the length of the record
  int len = 0;
  Loop read
  while (len = is.read (buffer))!=-1) {
   bops.write (buffer, 0, Len);
  }
  Converts the read content to byte array
  byte data[] = Bops.tobytearray ();
  Bops.flush ();
  Bops.close ();
  Is.close ();
  return data;
 }


I hope this article will help you with Java programming.

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.