Fix(DownloadUtils): Set Read Timeout Duration (#6504)

* Fix[DownloadUtils]: Set Read Timeout Duration
Pojav has not set a timeout for downloading files, which results in situations where file downloads do not throw an exception or terminate even after timing out.

* Update DownloadUtils.java
This commit is contained in:
MovTery 2025-04-19 02:56:13 +08:00 committed by GitHub
parent 42c1c98c21
commit 6eb830ba7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,6 +15,7 @@ import org.apache.commons.io.*;
@SuppressWarnings("IOStreamConstructor")
public class DownloadUtils {
public static final String USER_AGENT = Tools.APP_NAME;
private static final int TIME_OUT = 10000;
public static void download(String url, OutputStream os) throws IOException {
download(new URL(url), os);
@ -26,7 +27,8 @@ public class DownloadUtils {
// System.out.println("Connecting: " + url.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setConnectTimeout(10000);
conn.setConnectTimeout(TIME_OUT);
conn.setReadTimeout(TIME_OUT);
conn.setDoInput(true);
conn.connect();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
@ -67,6 +69,8 @@ public class DownloadUtils {
FileUtils.ensureParentDirectory(outputFile);
HttpURLConnection conn = (HttpURLConnection) new URL(urlInput).openConnection();
conn.setConnectTimeout(TIME_OUT);
conn.setReadTimeout(TIME_OUT);
InputStream readStr = conn.getInputStream();
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
int current;
@ -81,8 +85,9 @@ public class DownloadUtils {
monitor.updateProgress(overall, length);
}
conn.disconnect();
} catch (IOException e) {
throw new IOException("Unable to download from " + urlInput, e);
}
}
public static <T> T downloadStringCached(String url, String cacheName, ParseCallback<T> parseCallback) throws IOException, ParseException{