forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
32 lines (23 loc) · 1.07 KB
/
Copy pathMain.java
File metadata and controls
32 lines (23 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package modern.challenge;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.WRITE;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Downloading (please wait) ...");
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://demo.borland.com/testsite/downloads/downloadfile.php?file=Hello.txt&cd=attachment+filename"))
.build();
HttpResponse<Path> response = client.send(
request, HttpResponse.BodyHandlers
.ofFileDownload(Path.of(System.getProperty("user.dir")), CREATE, WRITE));
System.out.println("Status code: " + response.statusCode());
System.out.println("\n Body: " + response.body());
}
}