为了账号安全,请及时绑定邮箱和手机立即绑定

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();

}

}


正在回答

1 回答

举报

0/150
提交
取消
Android-Service系列之断点续传下载
  • 参与学习       20444    人
  • 解答问题       87    个

想升职加薪么?本章课程你值得拥有,满满的干货,学起来吧

进入课程

length = is.read(buffer) length =1 这个是什么原因啊

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信