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

2022-10-20,,,,

复制代码 代码如下:
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()); // 得到文件名
 }

 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();
  }
 }
}

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

下载本文的Word格式文档,以方便收藏与打印。