private void readResponseHeaders(HttpURLConnection conn) { info.disposition = conn.getHeaderField("Content-Disposition"); info.location = conn.getHeaderField("Content-Location"); info.mimeType = DLUtil.normalizeMimeType(conn.getContentType()); final String transferEncoding = conn.getHeaderField("Transfer-Encoding"); if (TextUtils.isEmpty(transferEncoding)) { try { info.totalBytes = Integer.parseInt(conn.getHeaderField("Content-Length")); LogUtils.e("安装包的大小:" + info.totalBytes); } catch (NumberFormatException e) { info.totalBytes = -1; } } else { info.totalBytes = -1; } if (info.totalBytes == -1 && (TextUtils.isEmpty(transferEncoding) || !transferEncoding.equalsIgnoreCase("chunked"))) throw new RuntimeException("Can not obtain size of download file."); if (TextUtils.isEmpty(info.fileName)) info.fileName = DLUtil.obtainFileName(info.realUrl, info.disposition, info.location); }
private void dlInit(HttpURLConnection conn, int code) throws Exception { readResponseHeaders(conn); // DLDBManager.getInstance(context).updateTaskInfo(info); try { LogUtils.e("保存数据库前安装包的大小:" + info.totalBytes); mDLInfoDao.update(info); } catch (SQLException e) { e.printStackTrace(); } if (!DLUtil.createFile(info.dirPath, info.fileName)) throw new DLException("Can not create file"); info.file = new File(info.dirPath, info.fileName); // if (info.file.exists() && info.file.length() == info.totalBytes) { // Log.d(TAG, "The file which we want to download was already here."); // return; // } if (info.hasListener) info.listener.onStart(info); EventBus.getDefault().post(info, "onStart"); switch (code) { case HTTP_OK: mPreviousTime = System.currentTimeMillis(); dlData(conn); break; case HTTP_PARTIAL: if (info.totalBytes <= 0) { mPreviousTime = System.currentTimeMillis(); dlData(conn); break; } if (info.isResume) { mPreviousTime = System.currentTimeMillis(); for (DLThreadInfo threadInfo : info.threads) { DLManager.getInstance(context).addDLThread(new DLThread(threadInfo, info, this)); } break; } dlDispatch(); break; } }