激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

香港云服务器
服務器之家 - 編程語言 - JAVA教程 - 命令行使用支持斷點續傳的java多線程下載器

命令行使用支持斷點續傳的java多線程下載器

2019-11-07 14:37java教程網 JAVA教程

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.zmynmublwnt.cn/200812/tools/HA_LeapFTP.rar"))
    .start();

  new Thread(
    new D(
      "http://www.zmynmublwnt.cn/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();
  }
 }
}

 

延伸 · 閱讀

精彩推薦
313
主站蜘蛛池模板: 激情小说激情电影 | 国产三级精品最新在线 | 羞羞视频.www在线观看 | 91真视频| 国产va在线观看 | 精品成人av一区二区在线播放 | 久久综合给合久久狠狠狠97色69 | 操操影视 | 毛片在线免费视频 | 久久精品视频网站 | 一区二区三区日本在线观看 | 天天碰夜夜操 | 天堂精品在线 | 欧美爱爱视频网站 | 久久久久久久一区二区三区 | 亚洲视频在线免费看 | 国产无遮挡一级毛片 | 久久久入口 | 91黄瓜视频| 4p一女两男做爰在线观看 | av观看网站 | 蜜桃免费在线 | 国产午夜亚洲精品理论片大丰影院 | 精品国产91一区二区三区 | 依人九九宗合九九九 | h视频免费在线观看 | 一区二区三区小视频 | 深夜福利久久久 | 精品亚洲夜色av98在线观看 | 国产一级一片免费播放 | 97久久曰曰久久久 | 国产精品一区二区视频 | 免费看一级片 | h色网站免费观看 | 视频h在线| 羞羞视频一区 | 色av网址| 欧美性生活视频免费 | 中文字幕偷拍 | 欧美精品久久久久久久久久 | 92看片淫黄大片欧美看国产片 |