[求助]断点续传的问题
这段代码从原理来说应该没问题了,但实际是不有问题的,请指出,谢谢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不论我设什么值都是从最开始下载起走的,也就是说,没有起到指定开始位置下载的作用,怎么办?