添加downloadOffText,检查下载上传数据是否完整

This commit is contained in:
huangzhuanghua
2016-10-25 09:32:39 +08:00
parent 18d88ab298
commit f3ee2d2cf0
23 changed files with 125 additions and 120 deletions

View File

@ -7,8 +7,6 @@ import android.util.Log;
import com.gh.common.util.HttpsUtils;
import com.gh.common.util.Utils;
import org.apache.http.HttpStatus;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
@ -52,66 +50,34 @@ public class DownloadThread extends Thread {
if (TextUtils.isEmpty(entry.getUrl())) {
listener.onStatusChanged(DownloadStatus.notfound);
Utils.log(DownloadThread.class.getSimpleName(),
"error-->url is empty");
Utils.log(DownloadThread.class.getSimpleName(), "error-->url is empty");
return;
}
URL url = new URL(entry.getUrl());
Utils.log("url = " + entry.getUrl());
HttpURLConnection connection;
if ("https".equals(url.getProtocol())) {
connection = HttpsUtils.getHttpsURLConnection(url);
} else {
connection = (HttpURLConnection) url.openConnection();
}
HttpURLConnection connection = openConnection(new URL(entry.getUrl()), targetFile.length());
connection.setRequestMethod("GET");
connection.setConnectTimeout(CONNECT_TIME);
connection.setReadTimeout(READ_TIME);
connection.setRequestProperty("RANGE",
"bytes=" + targetFile.length() + "-");
Utils.log(DownloadThread.class.getSimpleName(),
"startPosition-->" + targetFile.length());
//设置自动重定向
connection.setInstanceFollowRedirects(true);
Utils.log(DownloadThread.class.getSimpleName(), "startPosition-->" + targetFile.length());
int code = connection.getResponseCode();
Utils.log("code = " +code);
if (code == HttpStatus.SC_MOVED_PERMANENTLY
|| code == HttpStatus.SC_MOVED_TEMPORARILY) {
if (code == HttpURLConnection.HTTP_MOVED_PERM
|| code == HttpURLConnection.HTTP_MOVED_TEMP) {
//未自动重定向
String location = connection.getHeaderField("Location");
Utils.log("location = " + location);
url = new URL(location);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(CONNECT_TIME);
connection.setReadTimeout(READ_TIME);
connection.setRequestProperty("RANGE",
"bytes=" + targetFile.length() + "-");
Utils.log(DownloadThread.class.getSimpleName(),
"startPosition-->" + targetFile.length());
//设置自动重定向
connection.setInstanceFollowRedirects(true);
connection = openConnection(new URL(location), targetFile.length());
code = connection.getResponseCode();
}
if (code == HttpStatus.SC_NOT_FOUND) {
if (code == HttpURLConnection.HTTP_NOT_FOUND) {
// 404 Not Found
listener.onStatusChanged(DownloadStatus.notfound);
Utils.log(DownloadThread.class.getSimpleName(),
"error-->404 Not Found");
Utils.log(DownloadThread.class.getSimpleName(), "error-->404 Not Found");
return;
}
bis = new BufferedInputStream(connection.getInputStream());
if (targetFile.length() > 0) {
bos = new BufferedOutputStream(new FileOutputStream(entry.getPath(), true));
} else {
bos = new BufferedOutputStream(new FileOutputStream(entry.getPath()));
}
String eTag = connection.getHeaderField("ETag");
if (!TextUtils.isEmpty(eTag) && eTag.startsWith("\"") && eTag.endsWith("\"")) {
eTag = eTag.substring(1, eTag.length() - 1);
@ -122,8 +88,7 @@ public class DownloadThread extends Thread {
Utils.log("eTag = " + eTag);
Utils.log("eTag2 = " + eTag2);
listener.onStatusChanged(DownloadStatus.hijack);
Utils.log(DownloadThread.class.getSimpleName(),
"error-->链接被劫持");
Utils.log(DownloadThread.class.getSimpleName(), "error-->链接被劫持");
return;
}
@ -132,14 +97,20 @@ public class DownloadThread extends Thread {
if (entry.getSize() == 0) {
entry.setSize(conentLength);
DownloadDao.getInstance(context).newOrUpdate(entry);
Utils.log(DownloadThread.class.getSimpleName(),
"记录第一次长度");
Utils.log(DownloadThread.class.getSimpleName(), "记录第一次长度");
}
Utils.log(DownloadThread.class.getSimpleName(),
"progress:" + entry.getProgress() + "/curfilesize:"
+ targetFile.length() + "=====contentLength:"
+ conentLength + "/ totalSize:" + entry.getSize());
bis = new BufferedInputStream(connection.getInputStream());
if (targetFile.length() > 0) {
bos = new BufferedOutputStream(new FileOutputStream(entry.getPath(), true));
} else {
bos = new BufferedOutputStream(new FileOutputStream(entry.getPath()));
}
byte[] buffer = new byte[2048];
int len;
while ((len = bis.read(buffer)) != -1) {
@ -160,16 +131,15 @@ public class DownloadThread extends Thread {
listener.onStatusChanged(DownloadStatus.done);
}
} catch (Exception e) {
String errorMsg = Log.getStackTraceString(e);
// TODO 默认第一次错误自动恢复一次下载
//e.getMessage() will null error
String errorMsg = Log.getStackTraceString(e);
if (!TextUtils.isEmpty(e.getMessage()) && e.getMessage().contains("connection timeout")) {
listener.onStatusChanged(DownloadStatus.timeout, errorMsg);
} else {
listener.onStatusChanged(DownloadStatus.neterror, errorMsg);
}
Utils.log(DownloadThread.class.getSimpleName(),
"exception-->" + e.toString());
Utils.log(DownloadThread.class.getSimpleName(), "exception-->" + e.toString());
} finally {
if (bis != null) {
try {
@ -188,6 +158,23 @@ public class DownloadThread extends Thread {
}
}
private HttpURLConnection openConnection(URL url, long range) throws Exception {
HttpURLConnection connection;
if ("https".equals(url.getProtocol())) {
connection = HttpsUtils.getHttpsURLConnection(url);
} else {
connection = (HttpURLConnection) url.openConnection();
}
connection.setRequestMethod("GET");
connection.setConnectTimeout(CONNECT_TIME);
connection.setReadTimeout(READ_TIME);
connection.setDoInput(true);
connection.setRequestProperty("RANGE", "bytes=" + range + "-");
//设置自动重定向
connection.setInstanceFollowRedirects(true);
return connection;
}
public void setStatus(DownloadStatus status) {
this.status = status;
}