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);
[解决办法]
+1