length = is.read(buffer) length =1 這個是什么原因啊
public void run() {
// 數據庫插入線程信息
// 線程 下載位置 寫入位置
// 開始下載了
HttpURLConnection conn = null;
if (!mDao.isExists(threadInfo.getUrl(), threadInfo.getId())) {
mDao.insertThread(threadInfo);
}
try {
URL url = new URL(threadInfo.getUrl());
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setRequestMethod("GET");
int start = threadInfo.getStart() + threadInfo.getFinised();
conn.setRequestProperty("Range", "bytes=" + start + "-"
+ threadInfo.getEnd());
File file = new File(DownloadService.DOWNLOAD_PATH,
fileInfo.getName());
raf = new RandomAccessFile(file, "rwd");
raf.seek(start);// 設置文件的寫入位置
// 開始下載
Intent intent = new Intent(DownloadService.ACTION_UPDATE);
mFinished += threadInfo.getFinised();
// 部分下載 206
if (conn.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT) {// 讀取數據
is = conn.getInputStream();
byte[] buffer = new byte[1024 * 4];
int length = -1;
long time = System.currentTimeMillis();
while ((length = is.read(buffer)) != -1) {
raf.write(buffer, 0, length);
Log.e("length",length+"");
// 發送進度
mFinished += length;
Log.e("mFinished",mFinished+"");
if (System.currentTimeMillis() - time > 500) {
time = System.currentTimeMillis();
intent.putExtra("finished", mFinished * 100
/fileInfo.getLength());
Log.e("==1",intent.toString());?
context.sendBroadcast(intent);
}
Log.e("isPause",isPause+"");
if (isPause) {
mDao.updateThread(threadInfo.getUrl(),
threadInfo.getId(), mFinished);
return;
}
}
}
// 寫入到文件
// 下載暫停 保存進度
// 下載進度發送廣播
mDao.deleteThread(threadInfo.getUrl(), threadInfo.getId());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
}
}
2022-03-29
我也是碰到這個問題了的,下面是我的解決辦法,
shexyptebhbvusougjmcrxjumkbffqdlsdgftdcylfiouqxzdmabzvercuxxzvovtmmvwohoxqmgtxnafykkwdpovonztolxvmgrfrqsbdzjtvqvlfic