这段代码从原理来说应该没问题了,但实际是不有问题的,请指出,谢谢
package wanglei;
import java.io.*;
import java.net.*;
import java.util.*;
import java.io.RandomAccessFile ;
public class DownTry
{
 private static int BUFFER_SIZE = 8096*1024;//缓冲区大小
 String fileName ;
 String desturl ;
 URL url = null ;
 HttpURLConnection httpUrl = null ;
 BufferedInputStream bis = null ;
 BufferedOutputStream bos = null ;
 FileOutputStream fos = null ;
 byte[] buf = new byte[BUFFER_SIZE];
    int size = 0;
 public DownTry(String durl,String fileName) throws IOException
 {
  desturl = durl ;
  this.fileName = fileName ;
  
    url = new URL(desturl) ;
  
    httpUrl =(HttpURLConnection)url.openConnection();
    
    httpUrl.setRequestProperty("User-Agent","NetFox");
//  设置断点续传的开始位置
  httpUrl.setRequestProperty("RANGE","bytes=2048");
//  获得输入流
  InputStream input = httpUrl.getInputStream();
   
  RandomAccessFile ra =  new RandomAccessFile(this.fileName,"rw");
 
  long nPos = 0 ;
//  定位文件指针到nPos位置
  ra.seek(nPos);
  byte[] b = new byte[1024];
  int nRead;
//  从输入流中读入字节流,然后写到文件中
  while((nRead=input.read(b,0,1024)) > 0)
  {
  ra.write(b,0,nRead);
  System.out.println("wanglei") ;
  }
  ra.close() ;
  input.close() ;
  httpUrl.disconnect() ;
    
 
能成功运行 ,但有个问题是,我感觉没达到断点续传的作用,
//  设置断点续传的开始位置
  httpUrl.setRequestProperty("RANGE","bytes=2048");
bytes不论我设什么值都是从最开始下载起走的,也就是说,没有起到指定开始位置下载的作用,怎么办?

解决方案 »

  1.   

    你的是:
    httpUrl.setRequestProperty("RANGE","bytes=2048");
    建议:
    httpUrl.setRequestProperty("RANGE","bytes=2048-");
      

  2.   

    你的nPos一直是0,也就是说不管你怎么运行程序它都是从0位置开始传输文件,你要想做到断点续传应该在WHILE循环里面记录nPos的位置,当主动或被动从WHILE中跳出时,nPos记录着当前已经传完该文件的多少,然后下次重传时就从nPos位置开始传输就可以了