Procházet zdrojové kódy

Merge branch 'dev-xym-log' of Server/long-article-manage into master

xueyiming před 1 měsícem
rodič
revize
3dd89441a8

+ 11 - 4
long-article-server/src/main/java/com/tzld/piaoquan/longarticle/utils/other/VideoDownloader.java

@@ -21,6 +21,8 @@ import static com.tzld.piaoquan.longarticle.common.constants.ProxyConstant.*;
 @Slf4j
 public class VideoDownloader {
 
+    private static final long READ_TIMEOUT_SECONDS = 60; // 读取超时时间(秒)
+
     private static final int MAX_RETRIES = 3;
 
     public static String downloadCover(String outVideoId, String platform, String coverUrl) {
@@ -99,16 +101,21 @@ public class VideoDownloader {
 
             int responseCode = connection.getResponseCode();
             if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_PARTIAL) {
-                // 下载文件
                 try (InputStream inputStream = connection.getInputStream();
-                     OutputStream outputStream = Files.newOutputStream(file.toPath())) { // 追加模式
-                    byte[] buffer = new byte[1024 * 1024]; // 1MB buffer
+                     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;
+                        }
                     }
                 }
-                // 检查文件是否为空
                 if (file.length() != 0) {
                     return 0;
                 }