|
@@ -102,26 +102,21 @@ public class VideoDownloader {
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) {
|
|
|
long expectedSize = connection.getContentLengthLong();
|
|
|
- try (InputStream inputStream = connection.getInputStream();
|
|
|
- OutputStream outputStream = Files.newOutputStream(file.toPath())) {
|
|
|
- byte[] buffer = new byte[1024 * 1024];
|
|
|
- long lastReadTime = System.currentTimeMillis();
|
|
|
- int bytesRead;
|
|
|
- while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
- outputStream.write(buffer, 0, bytesRead);
|
|
|
- lastReadTime = System.currentTimeMillis();
|
|
|
- // 检查是否长时间没有读取到新数据
|
|
|
- if (System.currentTimeMillis() - lastReadTime > READ_TIMEOUT_SECONDS * 1000) {
|
|
|
- log.error("No data read for a long time, aborting download.");
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ InputStream inputStream = connection.getInputStream();
|
|
|
+ OutputStream outputStream = Files.newOutputStream(file.toPath());
|
|
|
+ byte[] buffer = new byte[1024 * 1024];
|
|
|
+ int bytesRead;
|
|
|
+ log.info("download start path={} expectedSize={}", path, expectedSize);
|
|
|
+ long size = 0;
|
|
|
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
+ size += bytesRead;
|
|
|
+ outputStream.write(buffer, 0, bytesRead);
|
|
|
}
|
|
|
- if (file.length() != expectedSize) {
|
|
|
+ log.info("download end path={}", path);
|
|
|
+ if (file.length() != 0) {
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return responseCode;
|
|
|
} catch (Exception e) {
|
|
|
log.error("download error", e);
|