添加链接404 Not Found 处理
This commit is contained in:
@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.volley.TimeoutError;
|
||||
import com.gh.common.util.HttpsUtils;
|
||||
import com.gh.common.util.Utils;
|
||||
|
||||
@ -15,6 +16,7 @@ import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class DownloadThread extends Thread {
|
||||
|
||||
@ -92,11 +94,20 @@ public class DownloadThread extends Thread {
|
||||
"startPosition-->" + targetFile.length());
|
||||
//设置自动重定向
|
||||
connection.setInstanceFollowRedirects(true);
|
||||
|
||||
code = connection.getResponseCode();
|
||||
}
|
||||
if (code == HttpStatus.SC_NOT_FOUND) {
|
||||
// 404 Not Found
|
||||
listener.onStatusChanged(DownloadStatus.notfound);
|
||||
Utils.log(DownloadThread.class.getSimpleName(),
|
||||
"error-->404 Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
|
||||
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
|
||||
long conentLength = connection.getContentLength();
|
||||
|
||||
String eTag = connection.getHeaderField("ETag");
|
||||
if (!TextUtils.isEmpty(eTag) && eTag.startsWith("\"") && eTag.endsWith("\"")) {
|
||||
eTag = eTag.substring(1, eTag.length() - 1);
|
||||
@ -109,45 +120,43 @@ public class DownloadThread extends Thread {
|
||||
listener.onStatusChanged(DownloadStatus.hijack);
|
||||
Utils.log(DownloadThread.class.getSimpleName(),
|
||||
"error-->链接被劫持");
|
||||
interrupt();
|
||||
} else {
|
||||
// 第一次下载记录文件长度
|
||||
if (entry.getSize() == 0) {
|
||||
entry.setSize(conentLength);
|
||||
DownloadDao.getInstance(context).newOrUpdate(entry);
|
||||
Utils.log(DownloadThread.class.getSimpleName(),
|
||||
"记录第一次长度");
|
||||
}
|
||||
Utils.log(DownloadThread.class.getSimpleName(),
|
||||
"progress:" + entry.getProgress() + "/curfilesize:"
|
||||
+ targetFile.length() + "=====contentLength:"
|
||||
+ conentLength + "/ totalSize:" + entry.getSize());
|
||||
|
||||
byte[] buffer = new byte[2048];
|
||||
int len;
|
||||
while ((len = bis.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, len);
|
||||
listener.onProgressChanged(targetFile.length(), len);
|
||||
if (status == DownloadStatus.pause
|
||||
|| status == DownloadStatus.cancel) {
|
||||
listener.onStatusChanged(status);
|
||||
break;
|
||||
}
|
||||
}
|
||||
bos.flush();
|
||||
Utils.log(DownloadThread.class.getSimpleName(),
|
||||
"flush==>" + targetFile.length() + ",progress==>"
|
||||
+ entry.getProgress() + ",size==>" + entry.getSize());
|
||||
return;
|
||||
}
|
||||
|
||||
bis.close();
|
||||
bos.close();
|
||||
// if (status != DownloadStatus.pause && status != DownloadStatus.cancel) {
|
||||
// listener.onStatusChanged(DownloadStatus.done);
|
||||
// }
|
||||
if (targetFile.length() == entry.getSize()) {
|
||||
listener.onStatusChanged(DownloadStatus.done);
|
||||
long conentLength = connection.getContentLength();
|
||||
// 第一次下载记录文件长度
|
||||
if (entry.getSize() == 0) {
|
||||
entry.setSize(conentLength);
|
||||
DownloadDao.getInstance(context).newOrUpdate(entry);
|
||||
Utils.log(DownloadThread.class.getSimpleName(),
|
||||
"记录第一次长度");
|
||||
}
|
||||
Utils.log(DownloadThread.class.getSimpleName(),
|
||||
"progress:" + entry.getProgress() + "/curfilesize:"
|
||||
+ targetFile.length() + "=====contentLength:"
|
||||
+ conentLength + "/ totalSize:" + entry.getSize());
|
||||
|
||||
byte[] buffer = new byte[2048];
|
||||
int len;
|
||||
while ((len = bis.read(buffer)) != -1) {
|
||||
bos.write(buffer, 0, len);
|
||||
listener.onProgressChanged(targetFile.length(), len);
|
||||
if (status == DownloadStatus.pause
|
||||
|| status == DownloadStatus.cancel) {
|
||||
listener.onStatusChanged(status);
|
||||
break;
|
||||
}
|
||||
}
|
||||
bos.flush();
|
||||
Utils.log(DownloadThread.class.getSimpleName(),
|
||||
"flush==>" + targetFile.length() + ",progress==>"
|
||||
+ entry.getProgress() + ",size==>" + entry.getSize());
|
||||
|
||||
bis.close();
|
||||
bos.close();
|
||||
if (targetFile.length() == entry.getSize()) {
|
||||
listener.onStatusChanged(DownloadStatus.done);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String errorMsg = Log.getStackTraceString(e);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user