import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
public class Downloading implements Runnable, Serializable{
private static final long serialVersionUID = 1L;
private String s1,s2; // 分别表示 URL地址/下载到本地目录
private boolean t=true;
public Downloading(String s1,String s2){
this.s1=s1;
this.s2=s2;
}
public static long filesize(){
File fis=new File(s2);
return fis.length();
}
public void sign(){
i++;
if(i%2==0) t=true;
else t=false;
}
public void run(){
int b=0;
long filebulk=0;
RandomAccessFile rafile = null;
try{
URL url=new URL(s1);
HttpURLConnection huc=(HttpURLConnection)url.openConnection();
filebulk = Long.parseLong(huc.getHeaderField("Content-Length"));
InputStream in=huc.getInputStream();
byte[] buffer = new byte[1024];
huc.setRequestProperty("RANGE", "bytes=" + filesize() + "-"); //运行时怎么总是报错???
rafile = new RandomAccessFile(s2, "rw");
rafile.seek(filesize());
while((b=in.read(buffer))!=-1&&t){
rafile.write(buffer,(int)filesize(),(int)filebulk);
}
in.close();
rafile.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Downloading hgf=new Downloading("http://192.168.0.28/050.rmvb","E:/256.rmvb");
Thread t=new Thread(hgf);
t.start();
}
}
//程序中有<1>处怎么运行时报错?
源文件如下: