国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

命令行使用支持?jǐn)帱c(diǎn)續(xù)傳的java多線程下載器

2019-11-26 15:41:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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.VeVB.COm:81/200812/tools/HA_LeapFTP.rar"))
    .start();

  new Thread(
    new D(
      "http://al.VeVB.COm: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); // 設(shè)置連接超時(shí)

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

  // 如果本地文件未下載完成,則斷點(diǎn)下載
  if (-1 != localFileSize && -1 != remoteFileSize
    && localFileSize < remoteFileSize) {
   file.seek(localFileSize); // 本地標(biāo)記
   get.addHeader("Range", "bytes=" + localFileSize + "-"
     + remoteFileSize); // 遠(yuǎn)程標(biāo)記
  }

  // 如果本地文件大小大于等于遠(yuǎn)程文件,則已經(jīng)下載完成
  if (-1 != localFileSize && localFileSize >= remoteFileSize) {
   return;
  }

  // 開(kāi)始下載
  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;

   // 顯示進(jìn)度
   new Thread(new Runnable(){
    @Override
    public void run() {
     try {
      while (file.length() < remoteFileSize) {
//       Runtime.getRuntime().exec("cmd cls");  // 聽(tīng)說(shuō)會(huì)另起個(gè)進(jìn)程
       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();

   // 開(kāi)始下載
   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();
 }

 // 得到遠(yuǎn)程文件大小
 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();
  }
 }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宾阳县| 偃师市| 井陉县| 越西县| 黄浦区| 阿拉善盟| 新蔡县| 寿光市| 塘沽区| 西安市| 灵川县| 临颍县| 阿尔山市| 开鲁县| 邹城市| 梓潼县| 洛隆县| 米易县| 马龙县| 都安| 祁阳县| 布尔津县| 平顺县| 沙田区| 内黄县| 平陆县| 尼玛县| 武定县| 建瓯市| 太白县| 汽车| 鹤岗市| 济宁市| 浮梁县| 巩义市| 安化县| 榆树市| 离岛区| 金坛市| 共和县| 青神县|