課程
/移動(dòng)開發(fā)
/Android
/Android-Service系列之多線程斷點(diǎn)續(xù)傳下載
用老師這種方式為什么下載下來的文件不能安裝呢?
2015-04-24
源自:Android-Service系列之多線程斷點(diǎn)續(xù)傳下載 2-4
正在回答
下載部分的讀取和寫入有問題,需要檢查下代碼
慕粉2041172413
/**
* 下載線程
*/
class DownloadThread extends Thread {
private ThreadInfo threadInfo; // 線程信息
// public boolean isFinished = false;
public DownloadThread(ThreadInfo threadInfo) {
this.threadInfo = threadInfo;
Log.i(tag, "thread info = " + threadInfo);
}
@Override
public void run() {
URL url = null;
HttpURLConnection con = null; // http鏈接
RandomAccessFile accessFile = null; // 下載文件
InputStream inputStream = null; // 輸入流
try {
int start = threadInfo.getStart() + threadInfo.getFinished(); // 讀取文件的位置
// int startPos = blockSize * (threadId - 1);//開始位置 ?
// int endPos = blockSize * threadId - 1;//結(jié)束位置?
?
// start 初始化下載鏈接
url = new URL(threadInfo.getUrl());
con = (HttpURLConnection) url.openConnection();
con.setAllowUserInteraction(true); ?
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setRequestProperty("Range", "bytes=" + start + "-"
+ threadInfo.getEnd()); // 設(shè)置讀取文件的位置,和結(jié)束位置
// end 初始化下載鏈接
// start 初始化下載到本地的文件
accessFile = new RandomAccessFile(new File(
mTaskInfo.getFilePath(), mTaskInfo.getFileName()),
"rwd");
accessFile.seek(start); // 設(shè)置開始寫入的位置
// end 初始化下載到本地的文件
int responseCode = con.getResponseCode();
if ((con.getResponseCode() == HttpURLConnection.HTTP_PARTIAL)
|| (con.getResponseCode() == HttpURLConnection.HTTP_OK)) {
inputStream = con.getInputStream();
int finished = threadInfo.getFinished(); // 已經(jīng)下載的長度
// int len = threadInfo.getEnd()-threadInfo.getStart();
// //本線程要下載的長度
int readLen = -1; // 讀取的長度
byte[] buffer = new byte[1024 * 4];
long time = System.currentTimeMillis();
// start 讀取輸入流寫入文件
while ((readLen = inputStream.read(buffer)) != -1) {
accessFile.write(buffer, 0, readLen);
// Log.i(tag, "readLen = " + readLen);
finished += readLen;
threadInfo.setFinished(threadInfo.getFinished()+readLen); // 設(shè)置已經(jīng)下載進(jìn)度
if (System.currentTimeMillis() - time > 2000) {
notifyProgress(threadInfo.getId(), threadInfo.getFinished()); // 每隔2秒通知下載進(jìn)度
time = System.currentTimeMillis();
// start 停止下載,保存進(jìn)度
if (isPause) {
Log.i(tag,
"pause name = " + mTaskInfo.getFileName());
notifyProgress(threadInfo.getId(), threadInfo.getFinished()); // 通知下載進(jìn)度
mThreadDao.updateThread(threadInfo.getUrl(),
threadInfo.getId(), threadInfo.getFinished()); // 更新數(shù)據(jù)庫對應(yīng)的線程信息
return;
// end 停止下載,保存進(jìn)度
// end 讀取輸入流寫入文件
// mThreadDao.updateThread(threadInfo.getUrl(),threadInfo.getId(),finished);
// isFinished = true;
// checkIsAllThreadFinished();
// broadcastFinished(threadInfo.getId(),finished);
notifyProgress(threadInfo.getId(), finished);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
} finally {
if (inputStream != null) {
inputStream.close();
if (accessFile != null) {
accessFile.close();
if (null != con) {
con.disconnect();
super.run();
舉報(bào)
本視頻教程主要代領(lǐng)我們要學(xué)習(xí)的多線程續(xù)傳下載程序的開發(fā)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-05-04
下載部分的讀取和寫入有問題,需要檢查下代碼
2016-12-08
/**
* 下載線程
*/
class DownloadThread extends Thread {
private ThreadInfo threadInfo; // 線程信息
// public boolean isFinished = false;
public DownloadThread(ThreadInfo threadInfo) {
this.threadInfo = threadInfo;
Log.i(tag, "thread info = " + threadInfo);
}
@Override
public void run() {
URL url = null;
HttpURLConnection con = null; // http鏈接
RandomAccessFile accessFile = null; // 下載文件
InputStream inputStream = null; // 輸入流
try {
int start = threadInfo.getStart() + threadInfo.getFinished(); // 讀取文件的位置
// int startPos = blockSize * (threadId - 1);//開始位置 ?
// int endPos = blockSize * threadId - 1;//結(jié)束位置?
?
// start 初始化下載鏈接
url = new URL(threadInfo.getUrl());
con = (HttpURLConnection) url.openConnection();
con.setAllowUserInteraction(true); ?
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setRequestProperty("Range", "bytes=" + start + "-"
+ threadInfo.getEnd()); // 設(shè)置讀取文件的位置,和結(jié)束位置
// end 初始化下載鏈接
// start 初始化下載到本地的文件
accessFile = new RandomAccessFile(new File(
mTaskInfo.getFilePath(), mTaskInfo.getFileName()),
"rwd");
accessFile.seek(start); // 設(shè)置開始寫入的位置
// end 初始化下載到本地的文件
int responseCode = con.getResponseCode();
if ((con.getResponseCode() == HttpURLConnection.HTTP_PARTIAL)
|| (con.getResponseCode() == HttpURLConnection.HTTP_OK)) {
inputStream = con.getInputStream();
int finished = threadInfo.getFinished(); // 已經(jīng)下載的長度
// int len = threadInfo.getEnd()-threadInfo.getStart();
// //本線程要下載的長度
int readLen = -1; // 讀取的長度
byte[] buffer = new byte[1024 * 4];
long time = System.currentTimeMillis();
// start 讀取輸入流寫入文件
while ((readLen = inputStream.read(buffer)) != -1) {
accessFile.write(buffer, 0, readLen);
// Log.i(tag, "readLen = " + readLen);
finished += readLen;
threadInfo.setFinished(threadInfo.getFinished()+readLen); // 設(shè)置已經(jīng)下載進(jìn)度
if (System.currentTimeMillis() - time > 2000) {
// Log.i(tag, "readLen = " + readLen);
notifyProgress(threadInfo.getId(), threadInfo.getFinished()); // 每隔2秒通知下載進(jìn)度
time = System.currentTimeMillis();
}
// start 停止下載,保存進(jìn)度
if (isPause) {
Log.i(tag,
"pause name = " + mTaskInfo.getFileName());
notifyProgress(threadInfo.getId(), threadInfo.getFinished()); // 通知下載進(jìn)度
mThreadDao.updateThread(threadInfo.getUrl(),
threadInfo.getId(), threadInfo.getFinished()); // 更新數(shù)據(jù)庫對應(yīng)的線程信息
return;
}
// end 停止下載,保存進(jìn)度
}
// end 讀取輸入流寫入文件
// mThreadDao.updateThread(threadInfo.getUrl(),threadInfo.getId(),finished);
// isFinished = true;
// checkIsAllThreadFinished();
// broadcastFinished(threadInfo.getId(),finished);
notifyProgress(threadInfo.getId(), finished);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (accessFile != null) {
accessFile.close();
}
if (null != con) {
con.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
super.run();
}
}