Skip to content

Commit 06fe5cb

Browse files
authored
Update ImageDownloader.java
1 parent b84b483 commit 06fe5cb

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/main/java/generateimages/ImageDownloader.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.file.Path;
1111
import java.nio.file.Paths;
1212
import java.nio.file.StandardCopyOption;
13+
import java.time.Duration;
1314

1415
public class ImageDownloader {
1516

@@ -18,8 +19,11 @@ public static void downloadImage(String imageUrl, String destinationFile)
1819
// Create an HttpClient
1920
HttpClient client = HttpClient.newHttpClient();
2021

21-
// Create an HttpRequest for the image URL
22-
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(imageUrl)).build();
22+
// Create an HttpRequest for the image URL with a timeout of 20 seconds
23+
HttpRequest request = HttpRequest.newBuilder()
24+
.uri(URI.create(imageUrl))
25+
.timeout(Duration.ofSeconds(20)) // Set timeout duration
26+
.build();
2327

2428
// Send the request and receive the response
2529
HttpResponse<InputStream> response =

0 commit comments

Comments
 (0)