本周任务是用java实现http的多线程(非断点续传)下载;
现在网络带宽动辄百兆的环境下,http下载工具相信已经很少人想得起了,大多数情况下浏览器自带的下载功能已经能够满足需要;
现在使用工具多是用于某些特定的协议比如BT比如磁力链,或者是迅雷/腾讯等自家的下载链接;
但是网龄比较大的网友可能对2000年前后拨号上网(56k modern),500kb的adsl时代记忆犹新,不稳定并且低速率的带宽使得多线程/断点续传成为当时的刚需;
先来看看JAVA怎么实现单线程下载的:
/** * */ package com.coderising.download; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; /** * @author Administrator * */ public class TestDownload { /** * @param args */ public static void main(String[] args) { String URL_PATH = "http://src.onlinedown.net/Public/images/bigsoftimg/120000/q113222.jpg"; String DEST_PATH = "D://bt/javaDownloadTest.jpg"; try { URL url = new URL(URL_PATH); URLConnection conn = url.openConnection(); int length = conn.getContentLength(); System.out.println("GetLenght returned="+length); InputStream inputStream = conn.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream(DEST_PATH); int len = 0; int totalLen = 0; byte[] data = new byte[1024]; while((len = inputStream.read(data)) != -1) { totalLen+=len; fileOutputStream.write(data, 0, len); } System.out.println("Total len="+totalLen); inputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }要点是:
1.URL中打开connection
2.从Connection中获得InputStream
3.从InputStream读取并写入到本地文件
单线程的情况下我们不太关心总体文件的长度,因为一直沿着InputStream读出来知道输入流的结尾就没错了;多线程的情况下我们需要注意的几点:
1.需要使用HTTP文件头设置请求的range,因此不能使用默认的URLConnection而是HttpUrlConnection
HttpURLConnection conn = (HttpURLConnection)this.url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(30*1000); conn.setRequestProperty("Range", "bytes="+startPos+"-"+endPos);2.从HttpUrlConnection 获取 总长度并合理分段 length = conn.getContentLength();3.多个线程同时启动下载并写入到同一个本地文件的不同位置,使用RandomAcccessFile File f = new File(this.filePath); try { if(!f.exists()){ f.createNewFile(); } RandomAccessFile raf = new RandomAccessFile(f, "rw"); byte[] ba = conn.read(startPos, endPos);//The byte Array read from connection inputStream System.out.println("["+this.getName()+"]ByteArray length="+ba.length+",endPos-startPos+1="+(endPos-startPos+1)); raf.seek(startPos); raf.write(ba, 0, endPos-startPos+1); finished = true; System.out.println("["+this.getName()+"]DownloadThread-startPos="+startPos+"-endPos="+endPos+":download completed"); } catch (IOException e) { e.printStackTrace(); } 其他要做的就是等待所有线程下载结束之后推出进程。