读取输入流循环提前结束
is = conn.getInputStream(); // 读取文件 int len =-1; byte[] buffer = new byte[4*1024]; long time = System.currentTimeMillis(); while ((len = is.read(buffer)) != -1) { // 写入文件 raf.write(buffer, 0, len); if (System.currentTimeMillis() - time > 500) { time = System.currentTimeMillis(); // 把下载的进度发送广播道Activity mFinished += len; intent.putExtra("progress", (mFinished * 100)/ mFileInfo.getLength()); Log.i("下载进度:", mFinished + "--" + mFileInfo.getLength() + "--" + (mFinished * 100) / mFileInfo.getLength() + "%"); mContext.sendBroadcast(intent); } // 下载暂停时,保存下载进度 if (isPause) { mDao.updataThread(mThreadInfo.getUrl(), mThreadInfo.getId(), mFinished); System.out.print("下载暂停"); return; } } // 下载结束后删除线程信息 mDao.deleteThread(mThreadInfo.getUrl(), mThreadInfo.getId()); System.out.print("下载完成"); }
网络内容获取区间设置也正确
conn.setRequestProperty("Range", "bytes=" + start + "-" + mThreadInfo.getEnd());
while循环每次都提前结束。