- 获取要下载的文件的 内容长度 。
- *根据条件(大小,速度等)进行 *划分 。
运行多个线程下载开始在不同位置的文件,
并将它们保存在不同的文件:myfile.part1
,myfile.part2
,…下载后,将零件合并为 一个文件 。
我尝试了以下代码来获取内容长度:
public Downloader(String path) throws IOException { int len = 0; URL url = new URL(path); URLConnection connectUrl = url.openConnection(); System.out.println(len = connectUrl.getContentLength()); System.out.println(connectUrl.getContentType()); InputStream input = connectUrl.getInputStream(); int i = len; int c = 0; System.out.println("=== Content ==="); while (((c = input.read()) != -1) && (--i > 0)) { System.out.print((char) c); } input.close(); }以下是加入文件的示例代码:
public void join(String FilePath) { long leninfile=0, leng=0; int count=1, data=0; try { File filename = new File(FilePath); RandomAccessFile outfile = new RandomAccessFile(filename,"rw"); while(true) { filename = new File(FilePath + count + ".sp"); if (filename.exists()) { RandomAccessFile infile = new RandomAccessFile(filename,"r"); data=infile.read(); while(data != -1) { outfile.write(data); data=infile.read(); } leng++; infile.close(); count++; } else break; } outfile.close(); } catch(Exception e) { e.printStackTrace(); }}


