这个是下载链接
private static final String urlStr="http://www.androidbook.com/akc/filestorage/android/documentfiles/3389/movie.mp4";以下是我的下载方法
public void run(){
HttpURLConnection connection=null;
RandomAccessFile raFile=null;
InputStream is=null;
try{
Log.i("TAG run url", "[url]="+urlStr);
URL url=new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
raFile=new RandomAccessFile(saveFile,"rwd");
raFile.seek(startPos+completeSize);
is=connection.getInputStream();
byte[] buffer=new byte[4096];
int length=-1;
while((length=is.read(buffer))!=-1){
raFile.write(buffer,0,length);
completeSize+=length;
dbManager.updataInfos(urlStr,completeSize);
Message message=Message.obtain();
message.what=1;
message.obj=urlStr;
message.arg1=length;
mHandler.sendMessage(message);
if(state==PAUSE){
return;
}
}
}catch(Exception e){
Log.i("TAG run error", e.toString());
}finally{
try{
is.close();
raFile.close();
connection.disconnect();
dbManager.closeDB();
Log.i("TAG download", "download finish");
}catch(Exception e){
}
}
我有个暂停按钮,一按暂停按钮,这个线程就会返回,再按下载按钮这个线程又会重新生成。我奇怪的是每次暂停后再按下载,http会自动从我暂定的那点继续下载(我表示十分奇怪)为什么不会重新下载的呢?
我觉得按道理是要重新开始下载。
private static final String urlStr="http://www.androidbook.com/akc/filestorage/android/documentfiles/3389/movie.mp4";以下是我的下载方法
public void run(){
HttpURLConnection connection=null;
RandomAccessFile raFile=null;
InputStream is=null;
try{
Log.i("TAG run url", "[url]="+urlStr);
URL url=new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
raFile=new RandomAccessFile(saveFile,"rwd");
raFile.seek(startPos+completeSize);
is=connection.getInputStream();
byte[] buffer=new byte[4096];
int length=-1;
while((length=is.read(buffer))!=-1){
raFile.write(buffer,0,length);
completeSize+=length;
dbManager.updataInfos(urlStr,completeSize);
Message message=Message.obtain();
message.what=1;
message.obj=urlStr;
message.arg1=length;
mHandler.sendMessage(message);
if(state==PAUSE){
return;
}
}
}catch(Exception e){
Log.i("TAG run error", e.toString());
}finally{
try{
is.close();
raFile.close();
connection.disconnect();
dbManager.closeDB();
Log.i("TAG download", "download finish");
}catch(Exception e){
}
}
我有个暂停按钮,一按暂停按钮,这个线程就会返回,再按下载按钮这个线程又会重新生成。我奇怪的是每次暂停后再按下载,http会自动从我暂定的那点继续下载(我表示十分奇怪)为什么不会重新下载的呢?
我觉得按道理是要重新开始下载。
raFile.seek(startPos+completeSize);
这一句改为:
raFile.seek(0);
可以不重新下载的呢
raFile.seek(startPos+completeSize);只是让文件在startPos+completeSize出读入。
你没看seek(startPos+completesize)这是开始位置
重新开始后startPos不为0了,你输出看一下
这才达到了断点续传的目的,
1楼说的对,你改为0,就是每次从0位置开始传送,就不断点了
按照道理是重新下载的,并且文件从seek(startPos+completesize)开始读入流。这种情况下,这个文件居然还是正常的,所以我觉得很奇怪,求解!