From 348a465cd82c33199e0f3e266558db37dd91d306 Mon Sep 17 00:00:00 2001 From: lyy <511455842@qq.com> Date: Fri, 14 Oct 2016 18:03:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=BB=E5=A1=9E=E5=8E=9F=E5=9B=A0=E6=9F=A5?= =?UTF-8?q?=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arialyy/downloadutil/util/DownLoadUtil.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java index 25c63b45..ee455d25 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java @@ -282,18 +282,19 @@ final class DownLoadUtil { } @Override public void run() { - long currentLocation = 0; + long currentLocation = 0; + HttpURLConnection conn = null; + InputStream is = null; try { Log.d(TAG, "线程_" + dEntity.threadId - + "_正在下载【" - + "开始位置 : " + + "_正在下载【开始位置 : " + dEntity.startLocation + ",结束位置:" + dEntity.endLocation + "】"); - URL url = new URL(dEntity.downloadUrl); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + URL url = new URL(dEntity.downloadUrl); + conn = (HttpURLConnection) url.openConnection(); //在头里面请求下载开始位置和结束位置 conn.setRequestProperty("Range", "bytes=" + dEntity.startLocation + "-" + dEntity.endLocation); @@ -305,7 +306,7 @@ final class DownLoadUtil { "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); conn.setConnectTimeout(TIME_OUT * 4); conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数 - InputStream is = conn.getInputStream(); + is = conn.getInputStream(); //创建可设置位置的文件 RandomAccessFile file = new RandomAccessFile(dEntity.tempFile, "rwd"); //设置每条线程写入文件的位置 @@ -328,7 +329,9 @@ final class DownLoadUtil { currentLocation += len; } file.close(); + //close 为阻塞的,需要使用线程池来处理 is.close(); + conn.disconnect(); if (isCancel) { cancel();