/** * 将下载好的文件从缓存目录移动到下载完成的目录 * * @param cachedFile * @param extension * @return */ private String mvFileToDownloadedDir(String cachedFile, String extension) { CommonUtilsConfig.LOGD("----- move cached file to + " + DOWNLOADED_FILE_DIR); File dir = new File(DOWNLOADED_FILE_DIR); if (!dir.exists() || !dir.isDirectory()) { dir.delete(); dir.mkdirs(); } File file = new File(cachedFile); String ext = TextUtils.isEmpty(extension) ? "" : "." + extension; File newFile = new File(dir.getAbsolutePath() + "/" + file.getName() + ext); // // 重命名成功 if (CommonUtilsRuntime.isExternalStorageAvailable() && file.renameTo(newFile)) { CommonUtilsConfig.LOGD( "----- move cached file to + " + newFile.getAbsolutePath() + " successfully InstallApp======="); return newFile.getAbsolutePath(); } // 如果重命名失败,则通过拷贝实现 InputStream is = null; OutputStream os = null; try { is = new FileInputStream(file); os = null; if (dir.getAbsolutePath().startsWith("/data/data")) { CommonUtilsConfig.LOGD( "open world readable file" + " successfully InstallApp=======" + file.getName() + ext); os = mContext.openFileOutput(file.getName() + ext, Context.MODE_WORLD_READABLE); } else { CommonUtilsConfig.LOGD("new FileOutputStream, InstallApp"); os = new FileOutputStream(newFile); } CommonUtilsConfig.LOGD( "----- move cached file to + " + newFile.getAbsolutePath() + " successfully InstallApp======="); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (FileUtil.copy(is, os)) { file.delete(); return newFile.getAbsolutePath(); } return null; }
@Override public String onInputStreamReturn(String requestUrl, InputStream is) { // if (!UtilsRuntime.isSDCardReady()) { // UtilsConfig.LOGD("return because unmount the sdcard"); // return null; // } if (DEBUG) { CommonUtilsConfig.LOGD(""); CommonUtilsConfig.LOGD("//-------------------------------------------------"); CommonUtilsConfig.LOGD("||"); CommonUtilsConfig.LOGD("|| [[FileDownloader::onInputStreamReturn]] : "); CommonUtilsConfig.LOGD("|| try to download [[BIG]] file with url : " + requestUrl); CommonUtilsConfig.LOGD("||"); CommonUtilsConfig.LOGD("\\-------------------------------------------------"); CommonUtilsConfig.LOGD(""); } if (is != null) { String saveUrl = mDownloadFilenameCreateListener != null ? mDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(requestUrl) : mDefaultDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(requestUrl); File bigCacheFile = new File(INPUT_STREAM_CACHE_PATH); if (!bigCacheFile.exists() || !bigCacheFile.isDirectory()) { bigCacheFile.delete(); bigCacheFile.mkdirs(); } long curTime = 0; if (DEBUG) { CommonUtilsConfig.LOGD( "try to download from inputstream to local path = " + INPUT_STREAM_CACHE_PATH + saveUrl + " for orgin URL : " + requestUrl); curTime = System.currentTimeMillis(); } // download file int totalSize = 0; try { totalSize = is.available(); } catch (Exception e) { e.printStackTrace(); } long downloadSize = 0; String savePath = null; String targetPath = INPUT_STREAM_CACHE_PATH + saveUrl; byte[] buffer = new byte[4096 * 2]; File f = new File(targetPath); int len; OutputStream os = null; boolean isClosed = false; try { if (f.exists()) { downloadSize = f.length(); } os = new FileOutputStream(f, true); while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); // add listener to Notify UI downloadSize += len; handleProcess(requestUrl, totalSize, (int) downloadSize); if (RUNTIME_CLOSE_SUPPORTED) { DownloadRequest r = findCacelRequest(requestUrl); if (r != null && r.mStatus == DownloadRequest.STATUS_CANCEL) { CommonUtilsConfig.LOGD("try to close is >>>>>>>>>>>>>>>>>>>>"); is.close(); isClosed = true; } } } savePath = targetPath; } catch (Exception ex) { ex.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (Exception e) { e.printStackTrace(); } } buffer = null; } // end download try { if (!isClosed) { is.close(); } } catch (Exception e) { e.printStackTrace(); } if (!isClosed && !TextUtils.isEmpty(savePath) && checkInputStreamDownloadFile(savePath)) { if (DEBUG) { long successTime = System.currentTimeMillis(); CommonUtilsConfig.LOGD( "[[onInputStreamReturn]] save Request url : " + saveUrl + " success ||||||| and the saved file size : " + FileUtil.convertStorage(new File(savePath).length()) + ", save cost time = " + (successTime - curTime) + "ms"); } return savePath; } else { // 遗留文件,用于下次的断点下载 if (DEBUG) { CommonUtilsConfig.LOGD( "===== failed to downlaod requestUrl : " + requestUrl + " beacuse the debug 断点 ====="); } return null; } } else { if (DEBUG) { CommonUtilsConfig.LOGD( "===== failed to downlaod requestUrl : " + requestUrl + " beacuse requestUrl is NULL ====="); } } return null; }