Anonymous
Реализация резюме для загрузки файлов через Интернет
Сообщение
Anonymous » 22 май 2024, 00:37
Мой приведенный ниже код для загрузки файла работает нормально, если не использовать возобновление. После прочтения дополнительного решения по реализации этого и решения проблемы я знаю, что должен проверить заголовок Last-Modified и установить его для подключения,
но я не могу этого сделать, потому что получаю ошибку, например, android. Невозможно установить свойство запроса после установления соединения или я получаю значение null< /code> для httpURLConnection,
я использую эту ссылку
возврат слушателя:
Код: Выделить всё
{
null=[HTTP/1.1 200 OK],
Cache-Control=[public],
Connection=[keep-alive],
Content-Length=[8037404],
Content-Md5=[VEqXHCc/Off7a6D0gRFpiQ==],
Content-Type=[image/jpeg],
Date=[Tue, 19 Jan 2016 07:24:36 GMT],
Etag=["544a971c273f39f7fb6ba0f481116989"],
Expires=[Sat, 29 Jul 2017 10:07:00 GMT],
Last-Modified=[Thu, 18 Dec 2014 08:44:34 GMT],
Server=[bws],
X-Android-Received-Millis=[1501063623576],
X-Android-Response-Source=[NETWORK 200],
X-Android-Selected-Protocol=[http/1.1],
X-Android-Sent-Millis=[1501063623532]
}
теперь, как я могу настроить это, чтобы иметь резюме для загрузки файлов?
Ссылка на GitHub
Код: Выделить всё
public void run() {
final URL url;
HttpURLConnection httpURLConnection = null;
try {
try {
url = new URL(mUrl);
String lastModified = httpURLConnection.getHeaderField("Last-Modified");
if (!lastModified.isEmpty()) {
httpURLConnection.setRequestProperty("If-Range", lastModified);
}
httpURLConnection = (HttpURLConnection) url.openConnection();
if (mFile.exists()) {
downloadedLength = mFile.length();
Log.e("downloadedLength ", downloadedLength + "");
httpURLConnection.setRequestProperty("Range", "bytes=" + downloadedLength + "-");
fileOutputStream = new FileOutputStream(mFile, true);
} else {
fileOutputStream = new FileOutputStream(mFile);
}
httpURLConnection.setConnectTimeout(30000);
httpURLConnection.setReadTimeout(30000);
httpURLConnection.setRequestMethod("GET");
} catch (IOException e) {
}
final int responseCode;
final int total;
try {
responseCode = httpURLConnection.getResponseCode();
total = httpURLConnection.getContentLength();
} catch (IOException e) {
e.printStackTrace();
Log.e("ER UPDATE ", e.getMessage());
}
if (responseCode == 200) {
try {
inputStream = httpURLConnection.getInputStream();
} catch (IOException e) {
e.printStackTrace();
Log.e("IOException ", e.getMessage());
}
final byte[] buffer = new byte[4 * 1024];
int length = -1;
int finished = 0;
long start = System.currentTimeMillis();
try {
while ((length = inputStream.read(buffer)) != -1) {
if (!isDownloading()) {
throw new CanceledException("canceled");
}
fileOutputStream.write(buffer, 0, length);
finished += length;
if (System.currentTimeMillis() - start > 1000) {
onDownloadProgressing(finished, total);
start = System.currentTimeMillis();
}
}
onDownloadCompleted();
} catch (IOException e) {
e.printStackTrace();
Log.e("ER UPDATE ", e.getMessage());
}
} else {
Log.e("responseCode ", responseCode + "");
}
} catch (DownloadException e) {
e.printStackTrace();
Log.e("ER UPDATE ", e.getMessage());
} catch (CanceledException e) {
e.printStackTrace();
Log.e("ER UPDATE ", e.getMessage());
}
}
а также я получаю код ответа 206 вместо 200
Подробнее здесь:
https://stackoverflow.com/questions/453 ... a-internet
1716327450
Anonymous
Мой приведенный ниже код для загрузки файла работает нормально, если не использовать возобновление. После прочтения дополнительного решения по реализации этого и решения проблемы я знаю, что должен проверить заголовок Last-Modified и установить его для подключения, но я не могу этого сделать, потому что получаю ошибку, например, android. Невозможно установить свойство запроса после установления соединения или я получаю значение null< /code> для httpURLConnection, я использую эту ссылку [code]getHeaderField[/code] возврат слушателя: [code]{ null=[HTTP/1.1 200 OK], Cache-Control=[public], Connection=[keep-alive], Content-Length=[8037404], Content-Md5=[VEqXHCc/Off7a6D0gRFpiQ==], Content-Type=[image/jpeg], Date=[Tue, 19 Jan 2016 07:24:36 GMT], Etag=["544a971c273f39f7fb6ba0f481116989"], Expires=[Sat, 29 Jul 2017 10:07:00 GMT], Last-Modified=[Thu, 18 Dec 2014 08:44:34 GMT], Server=[bws], X-Android-Received-Millis=[1501063623576], X-Android-Response-Source=[NETWORK 200], X-Android-Selected-Protocol=[http/1.1], X-Android-Sent-Millis=[1501063623532] } [/code] теперь, как я могу настроить это, чтобы иметь резюме для загрузки файлов? Ссылка на GitHub [code]public void run() { final URL url; HttpURLConnection httpURLConnection = null; try { try { url = new URL(mUrl); String lastModified = httpURLConnection.getHeaderField("Last-Modified"); if (!lastModified.isEmpty()) { httpURLConnection.setRequestProperty("If-Range", lastModified); } httpURLConnection = (HttpURLConnection) url.openConnection(); if (mFile.exists()) { downloadedLength = mFile.length(); Log.e("downloadedLength ", downloadedLength + ""); httpURLConnection.setRequestProperty("Range", "bytes=" + downloadedLength + "-"); fileOutputStream = new FileOutputStream(mFile, true); } else { fileOutputStream = new FileOutputStream(mFile); } httpURLConnection.setConnectTimeout(30000); httpURLConnection.setReadTimeout(30000); httpURLConnection.setRequestMethod("GET"); } catch (IOException e) { } final int responseCode; final int total; try { responseCode = httpURLConnection.getResponseCode(); total = httpURLConnection.getContentLength(); } catch (IOException e) { e.printStackTrace(); Log.e("ER UPDATE ", e.getMessage()); } if (responseCode == 200) { try { inputStream = httpURLConnection.getInputStream(); } catch (IOException e) { e.printStackTrace(); Log.e("IOException ", e.getMessage()); } final byte[] buffer = new byte[4 * 1024]; int length = -1; int finished = 0; long start = System.currentTimeMillis(); try { while ((length = inputStream.read(buffer)) != -1) { if (!isDownloading()) { throw new CanceledException("canceled"); } fileOutputStream.write(buffer, 0, length); finished += length; if (System.currentTimeMillis() - start > 1000) { onDownloadProgressing(finished, total); start = System.currentTimeMillis(); } } onDownloadCompleted(); } catch (IOException e) { e.printStackTrace(); Log.e("ER UPDATE ", e.getMessage()); } } else { Log.e("responseCode ", responseCode + ""); } } catch (DownloadException e) { e.printStackTrace(); Log.e("ER UPDATE ", e.getMessage()); } catch (CanceledException e) { e.printStackTrace(); Log.e("ER UPDATE ", e.getMessage()); } } [/code] а также я получаю код ответа 206 вместо 200 Подробнее здесь: [url]https://stackoverflow.com/questions/45324253/implementing-resume-for-download-files-via-internet[/url]