Skip to content

Commit 72e3b36

Browse files
authored
Merge pull request #367 from lhDream/master
Increase file download timeout
2 parents e0a5206 + ab45b04 commit 72e3b36

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ It was born while teaching to my students how to build and distribute their Java
1515

1616
- [AstroImageJ](http://astroimagej.com/)
1717
- [Astro Pixel Processor](https://www.astropixelprocessor.com/)
18+
- [Drifty](https://github.com/SaptarshiSarkar12/Drifty)
1819
- [GistFX](https://github.com/RedmondSims/GistFX)
1920
- [Spektar Design Lab](https://spektar.io/)
2021

src/main/java/io/github/fvarrui/javapackager/maven/ResolveLicenseFromPOM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected File doApply(Packager packager) {
4242
} catch (MalformedURLException e) {
4343
Logger.error("Invalid license URL specified: " + urlStr);
4444
licenseFile = null;
45-
} catch (IOException e) {
45+
} catch (Exception e) {
4646
Logger.error("Cannot download license from " + urlStr);
4747
licenseFile = null;
4848
}

src/main/java/io/github/fvarrui/javapackager/utils/FileUtils.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.FileOutputStream;
1515
import java.io.IOException;
1616
import java.io.InputStream;
17+
import java.net.HttpURLConnection;
1718
import java.net.URL;
1819
import java.nio.charset.StandardCharsets;
1920
import java.nio.file.Files;
@@ -313,8 +314,9 @@ public static File findFirstFile(File searchFolder, String regex) {
313314
* @param file File to copy the downloaded resource
314315
* @throws IOException Resource cannot be copied/downloaded
315316
*/
316-
public static void downloadFromUrl(URL url, File file) throws IOException {
317-
org.apache.commons.io.FileUtils.copyURLToFile(url, file);
317+
public static void downloadFromUrl(URL url, File file) throws Exception {
318+
// org.apache.commons.io.FileUtils.copyURLToFile(url, file);
319+
copyUrlToFile(url,file);
318320
Logger.info("File downloaded from [" + url + "] to [" + file.getAbsolutePath() + "]");
319321
}
320322

@@ -325,7 +327,7 @@ public static void downloadFromUrl(URL url, File file) throws IOException {
325327
* @param file File to copy the downloaded resource
326328
* @throws IOException Resource cannot be copied/downloaded
327329
*/
328-
public static void downloadFromUrl(String url, File file) throws IOException {
330+
public static void downloadFromUrl(String url, File file) throws Exception {
329331
downloadFromUrl(new URL(url), file);
330332
}
331333

@@ -366,4 +368,36 @@ public static void writeStringToFileWithBOM(File output, String data) throws Exc
366368
fileOutputStream.close();
367369
}
368370

371+
/**
372+
*
373+
* @param url URL to download
374+
* @param file File to copy the downloaded resource
375+
* @throws Exception if something goes wrong
376+
*/
377+
public static void copyUrlToFile(URL url,File file) throws Exception {
378+
copyUrlToFile(url,file,30000);
379+
}
380+
381+
/**
382+
*
383+
* @param url URL to download
384+
* @param file File to copy the downloaded resource
385+
* @param timeout Download timeout (ms)
386+
* @throws Exception if something goes wrong
387+
*/
388+
public static void copyUrlToFile(URL url,File file,int timeout) throws Exception {
389+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
390+
connection.setConnectTimeout(timeout);
391+
connection.setReadTimeout(timeout);
392+
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
393+
try (InputStream in = connection.getInputStream();
394+
FileOutputStream out = new FileOutputStream(file)) {
395+
IOUtils.copy(in, out);
396+
}
397+
} else {
398+
Logger.warn("Failed to download the file. Response code: " + connection.getResponseCode());
399+
}
400+
connection.disconnect();
401+
}
402+
369403
}

0 commit comments

Comments
 (0)