服务器之家

服务器之家 > 正文

命令行使用支持断点续传的java多线程下载器

时间:2019-11-07 14:37     来源/作者:java教程网
代码如下:

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://www.zzvips.com/200812/tools/HA_LeapFTP.rar"))
    .start();

  new Thread(
    new D(
      "http://www.zzvips.com/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()); // 得到文件名
 }

 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); // 设置连接超时

  long localFileSize = this.getLocalFileSize();
  final long remoteFileSize = this.getRemoteFileSize();

  // 如果本地文件未下载完成,则断点下载
  if (-1 != localFileSize && -1 != remoteFileSize
    && localFileSize < remoteFileSize) {
   file.seek(localFileSize); // 本地标记
   get.addHeader("Range", "bytes=" + localFileSize + "-"
     + remoteFileSize); // 远程标记
  }

  // 如果本地文件大小大于等于远程文件,则已经下载完成
  if (-1 != localFileSize && localFileSize >= remoteFileSize) {
   return;
  }

  // 开始下载
  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;

   // 显示进度
   new Thread(new Runnable(){
    @Override
    public void run() {
     try {
      while (file.length() < remoteFileSize) {
//       Runtime.getRuntime().exec("cmd cls");  // 听说会另起个进程
       System.out.println(fileName
         + "已下载:\t"
         + new DecimalFormat("0.00%").format(file
           .length() / (double) remoteFileSize));
       Thread.sleep(5000);
      }
     } catch (IOException e) {
      e.printStackTrace();
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
   }).start();

   // 开始下载
   while (-1 != (len = in.read(by))) {
    file.write(by, 0, len);
   }

   in.close();
   client.getConnectionManager().shutdown();
  }
 }

 // 得到本地文件大小
 private long getLocalFileSize() {
  File file = new File(PATH + File.separator + this.fileName);
  if (!file.exists()) {
   return -1l;
  }

  return file.length();
 }

 // 得到远程文件大小
 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下载完成");
  } catch (ClientProtocolException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

 

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
返回顶部