亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

下載下來的文件不能安裝

用老師這種方式為什么下載下來的文件不能安裝呢?

正在回答

2 回答

下載部分的讀取和寫入有問題,需要檢查下代碼

0 回復 有任何疑惑可以回復我~
#1

慕粉2041172413

是嚴格按照視頻上的讀取和寫入的呀,還是不能安裝,文件大小也是正常的
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;//結束位置?

?

// 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()); // 設置讀取文件的位置,和結束位置

// end 初始化下載鏈接

// start 初始化下載到本地的文件

accessFile = new RandomAccessFile(new File(

mTaskInfo.getFilePath(), mTaskInfo.getFileName()),

"rwd");

accessFile.seek(start); // 設置開始寫入的位置

// end 初始化下載到本地的文件

int responseCode = con.getResponseCode();

if ((con.getResponseCode() == HttpURLConnection.HTTP_PARTIAL)

|| (con.getResponseCode() == HttpURLConnection.HTTP_OK)) {

inputStream = con.getInputStream();

int finished = threadInfo.getFinished(); // 已經下載的長度

// 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); // 設置已經下載進度

if (System.currentTimeMillis() - time > 2000) {

// Log.i(tag, "readLen = " + readLen);

notifyProgress(threadInfo.getId(), threadInfo.getFinished()); // 每隔2秒通知下載進度

time = System.currentTimeMillis();

}

// start 停止下載,保存進度

if (isPause) {

Log.i(tag,

"pause name = " + mTaskInfo.getFileName());

notifyProgress(threadInfo.getId(), threadInfo.getFinished()); // 通知下載進度

mThreadDao.updateThread(threadInfo.getUrl(),

threadInfo.getId(), threadInfo.getFinished()); // 更新數據庫對應的線程信息

return;

}

// end 停止下載,保存進度

}

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

}

}


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android-Service系列之多線程斷點續傳下載
  • 參與學習       21809    人
  • 解答問題       62    個

本視頻教程主要代領我們要學習的多線程續傳下載程序的開發

進入課程

下載下來的文件不能安裝

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號