URL url=null;
url = new URL(address);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestProperty("RANGE", "bytes="+pos+"-");
InputStream is=conn.getInputStream();
File file=new File(dest);
RandomAccessFile raf=new RandomAccessFile(file, "rw");
raf.seek(pos);
int n=-1;
byte[] buffer=new byte[8*1024];//8K
//读不尽的情况
while((n=is.read(buffer))!=-1){
raf.write(buffer,0,n);
pos+=n;
}
if(pos==fileLength){
System.out.println(pos+"/"+fileLength+"(100%)");
}
if(is!=null){
is.close();
}
if(raf!=null){
raf.close();
}程序功能就是下载指定链接的文件,下载的时候每次都是以接近10%的速度在下载,可是到了最后的99% 为什么连续走了三次才下载完,要知道我是每隔5s打印一次,这就是接近15s,百思不得其解,望大神解惑!java
url = new URL(address);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestProperty("RANGE", "bytes="+pos+"-");
InputStream is=conn.getInputStream();
File file=new File(dest);
RandomAccessFile raf=new RandomAccessFile(file, "rw");
raf.seek(pos);
int n=-1;
byte[] buffer=new byte[8*1024];//8K
//读不尽的情况
while((n=is.read(buffer))!=-1){
raf.write(buffer,0,n);
pos+=n;
}
if(pos==fileLength){
System.out.println(pos+"/"+fileLength+"(100%)");
}
if(is!=null){
is.close();
}
if(raf!=null){
raf.close();
}程序功能就是下载指定链接的文件,下载的时候每次都是以接近10%的速度在下载,可是到了最后的99% 为什么连续走了三次才下载完,要知道我是每隔5s打印一次,这就是接近15s,百思不得其解,望大神解惑!java
不知道你打印语句在什么地方,在这里,第一现场,打印一下看看就知道了while((n=is.read(buffer))!=-1){
raf.write(buffer,0,n);
pos+=n;
System.out.println(pos);
}