所谓断点下载,就是通过多个线程去下载网络文件,而不是用一条线程去下载,采用多线程断点下载可以极大的提高下载速度【这点毫无疑问】
原理是这样的: 首先通过网络文件长度的判断,来生成一个大小相同的本地文件,在这个里面主要用到了一个RandomAccessFile,这个类主要来控制每个线程对本地文件操作的时候要与服务器端的文件指针始终保持一致,通过计算每个线程下载的起始位置、结束位置,控制每个线程对本地文件的位置,这样通过多线程下载的文件才可以连接起来。。看代码
import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.HashSet; /** * 多线程断点下载demo * @author jone * * 2014-4-11 * * */ public class MutilThreadDownload { // 设定为需要三个线程来分段下载 int threadnum = 3; public static void main(String[] args) throws Exception { new MutilThreadDownload().download( "http://...." ); } private void download(String string) throws Exception { URL url = new URL( string ); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout( 5000 ); conn.setRequestMethod( "get" ); //获取网络文件长度 int len = conn.getContentLength(); File file = new File( "resre" ); // 随机文件类。。主要对file进行一个封转,完成具体的文件指针移动,保证本地文件与网络文件具有相同的文件指针,这样不同的线程下载的数据才可以连接到一起 RandomAccessFile file2 = new RandomAccessFile( file, "rws" ); file2.setLength( len ); file2.close(); // 每个线程下载block int block = len % threadnum == 0 ? len % threadnum : len % threadnum + 1; for ( int i = 0; i < threadnum; i++ ) { new DownloadThread( i, block, url, file ).start(); } } //一个线程下载过程 class DownloadThread extends Thread { int thidid, block; URL url; File file; /** * * @param i 线程id * @param block 一个线程下载的block块 * @param url 网络文件的url * @param file 本地文件 */ public DownloadThread(int i, int block, URL url, File file) { this.thidid = i; this.block = block; this.url = url; this.file = file; } @Override public void run() { try { /** * 运行之前要计算该条线程下载的起始位置、结束位置 * 将文件指针移动到该条线程所控制的本地文件的起始位置 * 设置下载网络文件的间距【通过Range头属性设置】 */ int start = thidid * block; int end = (thidid + 1) * block - 1; RandomAccessFile file2 = new RandomAccessFile( file, "rws" ); file2.seek( start ); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout( 5000 ); conn.setRequestMethod( "get" ); conn.setRequestProperty( "Range", "bytes=" + start + "-" + end ); //如果采用分段下载返回的状态码是206 if (conn.getResponseCode() == 206) { InputStream stream = conn.getInputStream(); byte[] buff = new byte[1024]; int len = 0; if ((len = stream.read( buff )) != -1) { file2.write( buff, 0, len ); } file2.close(); stream.close(); } } catch (Exception e) { // TODO: handle exception } super.run(); } } }