Command line with support for breakpoint continuation of the Java multithreaded download

  • 2020-04-01 02:56:15
  • OfStack


package org.load.download;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.text.DecimalFormat;
import org.apache.http.HttpEntity;
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 org.apache.http.impl.client.DefaultHttpClient;
public class Download {
 public static void main(String[] args) {
  new Thread(new D("http://al.jb51.net:81/200812/tools/HA_LeapFTP.rar"))
    .start();

  new Thread(
    new D(
      "http://al.jb51.net:81/200812/tools/HA_LeapFTP.rar"))
    .start();
 }
}
class D implements Runnable {
 private static final String PATH = "E:\download";
 private String url;
 private String fileName = null;
 static {
  if (!new File(PATH).exists()) {
   new File(PATH).mkdirs();
  }
 }
 public D(String url) {
  this.url = url;
  this.fileName = this.url.substring(this.url.lastIndexOf('/') + 1,
    this.url.length()); //Get the file name
 }
 public void download() throws ClientProtocolException, IOException {
  final RandomAccessFile file = new RandomAccessFile(this.PATH + File.separator
    + this.fileName, "rw");
  HttpClient client = new DefaultHttpClient();
  HttpGet get = new HttpGet(this.url);
//  client.getParams().setParameter("http.socket.timeout", 5000); //  Set connection timeout 
  long localFileSize = this.getLocalFileSize();
  final long remoteFileSize = this.getRemoteFileSize();
  //Breakpoint downloads if the local file is not downloaded
  if (-1 != localFileSize && -1 != remoteFileSize
    && localFileSize < remoteFileSize) {
   file.seek(localFileSize); //Local mark
   get.addHeader("Range", "bytes=" + localFileSize + "-"
     + remoteFileSize); //Remote tag
  }
  //If the local file size is greater than or equal to the remote file, the download is complete
  if (-1 != localFileSize && localFileSize >= remoteFileSize) {
   return;
  }
  //Start the download
  HttpResponse response = client.execute(get);
  if (300 >= response.getStatusLine().getStatusCode()) {
   HttpEntity en = response.getEntity();
   InputStream in = en.getContent();
   byte[] by = new byte[512];
   int len = -1;

   //According to schedule
   new Thread(new Runnable(){
    @Override
    public void run() {
     try {
      while (file.length() < remoteFileSize) {
//       Runtime.getRuntime().exec("cmd cls");  //  I heard there will be another process 
       System.out.println(fileName
         + " Download: t"
         + new DecimalFormat("0.00%").format(file
           .length() / (double) remoteFileSize));
       Thread.sleep(5000);
      }
     } catch (IOException e) {
      e.printStackTrace();
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
   }).start();

   //Start the download
   while (-1 != (len = in.read(by))) {
    file.write(by, 0, len);
   }
   in.close();
   client.getConnectionManager().shutdown();
  }
 }
 //Gets the local file size
 private long getLocalFileSize() {
  File file = new File(PATH + File.separator + this.fileName);
  if (!file.exists()) {
   return -1l;
  }
  return file.length();
 }
 //Gets the remote file size
 private long getRemoteFileSize() throws ClientProtocolException,
   IOException {
  HttpClient client = new DefaultHttpClient();
  HttpGet get = new HttpGet(this.url);
  client.getParams().setParameter("http.socket.timeout", 5000);
  HttpResponse response = client.execute(get);
  if (200 == response.getStatusLine().getStatusCode()
    || 300 >= response.getStatusLine().getStatusCode()) {
   HttpEntity en = response.getEntity();
   return en.getContentLength();
  }
  return -1l;
 }
 @Override
 public void run() {
  try {
   download();
   System.out.println(this.fileName + "t The download is complete ");
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}


Related articles: