Java——HTTP多线程下载,端口侦听和自启动服务

一个网友正好需要这个东西,我就把几个技术整合到了一起。包括三个部分,实现时也是逐个做到的

多线程的文件下载,HTTP协议

把这个功能做成一个HTTP的服务,侦听在某个端口上,方便非Java的系统使用

把这个功能封装为一个Windows服务,在机器启动时可以自动启动

我们逐个看程序。

一、多线程下载

这个主要使用了HTTP协议里面的一个Range参数,他设置了你读取数据的其实位置和终止位置。 经常使用flashget的用户在查看连接的详细信息时,应该经常看到这个东西。比如

Range:bytes=100-2000

代表从100个字节的位置开始读取,到2000个字节的位置结束,应读取1900个字节。

程序首先拿到文件的长度,然后分配几个线程去分别读取各自的一段,使用了

RandomAccessFile

进行随机位置的读写。

下面是完整的下载的代码。

package net.java2000.tools;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;
/**
* HTTP的多线程下载工具。
*
* @author 赵学庆 www.java2000.net
*/
public class HTTPDownloader extends Thread {
  // 要下载的页面
  private String page;
  // 保存的路径
  private String savePath;
  // 线程数
  private int threadNumber = 2;
  // 来源地址
  private String referer;
  // 最小的块尺寸。如果文件尺寸除以线程数小于这个,则会减少线程数。
  private int MIN_BLOCK = 10 * 1024;
  public static void main(String[] args) throws Exception {
   HTTPDownloader d = new HTTPDownloader("http://www.xxxx.net/xxxx.rar", "d://xxxx.rar", 10);
   d.down();
  }
  public void run() {
   try {
    down();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  /**
  * 下载操作
  *
  * @throws Exception
  */
  public void down() throws Exception {
   URL url = new URL(page); // 创建URL
   URLConnection con = url.openConnection(); // 建立连接
   int contentLen = con.getContentLength(); // 获得资源长度
   if (contentLen / MIN_BLOCK + 1 < threadNumber) {
    threadNumber = contentLen / MIN_BLOCK + 1; // 调整下载线程数
   }
   if (threadNumber > 10) {
    threadNumber = 10;
   }
   int begin = 0;
   int step = contentLen / threadNumber;
   int end = 0;
   for (int i = 0; i < threadNumber; i++) {
    end += step;
    if (end > contentLen) {
     end = contentLen;
    }
    new HTTPDownloaderThread(this, i, begin, end).start();
    begin = end;
   }
  }
  public HTTPDownloader() {
  }
  /**
  * 下载
  *
  * @param page 被下载的页面
  * @param savePath 保存的路径
  */
  public HTTPDownloader(String page, String savePath) {
   this(page, savePath, 10);

//来自互联网//
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值