基于http形式的单线程,可中断重传的文件下载方式。
实现主要思路,其实要实现http下载形式的断点重传文件,主要是对http请求的首部字段的Rang字段的应用。可以通过上次下载
断开的位置开始继续下载,HTTP协议中,可以在请求报文头中加入Range段,来表示客户机希望从何处继续下载。
这是一个普通的下载请求:
-
GET /test.txt HTTP/1.1
-
Accept:*/*
-
Referer:http://127.0.0.1
-
Accept-Language:zh-cn
-
Accept-Encoding:gzip,deflate
-
Agent:Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.2;.NET CLR 2.0.50727)
-
Host:127.0.0.1
-
Connection:Keep-Alive
这表示从9988
字节开始断点续传(加入了Range:bytes=1024-):
-
GET /test.txt HTTP/1.1
-
Accept:*/*
-
Referer:http://127.0.0.1
-
Accept-Language:zh-cn
-
Accept-Encoding:gzip,deflate
-
User-Agent:Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.2;.NET CLR 2.0.50727)
-
Host:127.0.0.1
-
Range:bytes=9988-
-
Connection:Keep-Alive
下面给出代码
public class PointUploadOne { public PointUploadOne() { } private boolean FileExist(String pathAndFile) // 确定文件是否已经下载,但没有下载完成 { File file = new File(pathAndFile); return file.exists(); } private long FileSize(String pathAndFile) // 确定已经下载了的文件大小 { File file = new File(pathAndFile); return file.length(); } private void FileRename(String fName, String nName) // 将下载完全的文件更名,去掉.tp名 { File file = new File(fName); file.renameTo(new File(nName)); file.delete(); } public static void main(String[] args) { URL url = null; HttpURLConnection urlc = null; DataOutputStream dos = null; BufferedInputStream bis = null; FileOutputStream fos = null; String localFile = "D:\\nike.png"; // 文件保存的地方及文件名,具体情况可以改 String localFile_bak = localFile + " .tp "; // 未下载完文件加.tp扩展名,以便于区别 PointUploadOne gco = new PointUploadOne(); long fileSize = 0; long start = System.currentTimeMillis(); int len = 0; byte[] bt = new byte[1024]; // byte[] buffer=new byte[50*1024]; RandomAccessFile raFile = null; long TotalSize = 0; // 要下载的文件总大小 try { url = new URL("http://39.108.74.219:90/images/aj001.png"); urlc = (HttpURLConnection) url.openConnection(); String headerField = urlc.getHeaderField("Content-Length"); InputStream in = urlc.getInputStream(); TotalSize = Long.parseLong(headerField); System.out.println(" 下载文件大小为: " + TotalSize); urlc.disconnect(); // 先断开,下面再连接,否则下面会报已经连接的错误 urlc = (HttpURLConnection) url.openConnection(); // 确定文件是否存在 if (gco.FileExist(localFile_bak)) // 采用断点续传,这里的依据是看下载文件是否在本地有.tp有扩展名同名文件 { System.out.println(" 文件续传中… "); fileSize = gco.FileSize(localFile_bak); // 取得文件在小,以便确定随机写入的位置 System.out.println(" fileSize: " + fileSize); // 设置User-Agent // urlc.setRequestProperty("User-Agent","NetFox"); // 设置断点续传的开始位置 urlc.setRequestProperty("RANGE", "bytes=" + fileSize + "-"); // urlc.setRequestProperty("RANGE", "bytes="+fileSize); // 这样写不行,不能少了这个"-". // 设置接受信息 urlc.setRequestProperty("Accept", "image/gif,image/x-xbitmap,application/msword,*/*"); raFile = new RandomAccessFile(localFile_bak, "rw"); // 随机方位读取 raFile.seek(fileSize); // 定位指针到fileSize位置 bis = new BufferedInputStream(urlc.getInputStream()); while ((len = bis.read(bt)) > 0) // 循环获取文件 { raFile.write(bt, 0, len); // buffer=buffer+bt; // System. } System.out.println(" 文件续传接收完毕! "); } else // 采用原始下载 { fos = new FileOutputStream(localFile_bak); // 没有下载完毕就将文件的扩展名命名.bak dos = new DataOutputStream(fos); bis = new BufferedInputStream(urlc.getInputStream()); System.out.println(" 正在接收文件… "); int test = 0; while ((len = bis.read(bt)) > 0) // 循环获取文件 { dos.write(bt, 0, len); test++; if (test == 50) // 这里是测试,你可以删除这里,就可以正常下载了 break; } // System.out.println("文件正常接收完毕!"); } System.out.println(" 共用时: " + (System.currentTimeMillis() - start) / 1000 + "ms"); if (bis != null) bis.close(); if (dos != null) dos.close(); if (fos != null) fos.close(); if (raFile != null) raFile.close(); System.out.println(" localFile_bak: " + gco.FileSize(localFile_bak)); if (gco.FileSize(localFile_bak) == TotalSize) // 下载完毕后,将文件重命名 { gco.FileRename(localFile_bak, localFile); } System.exit(0); } catch (Exception e) { try { if (bis != null) bis.close(); if (dos != null) dos.close(); if (fos != null) fos.close(); if (raFile != null) raFile.close(); } catch (IOException f) { f.printStackTrace(); } e.printStackTrace(); } System.exit(0); } }
后面一遍文章将给出多线程,可中断文件下载的形式。