# (DD06) Business Logic — Example.downloadFile() [13 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `io.github.biezhi.java11.http.Example` |
| Layer | Utility (Package: `io.github.biezhi.java11.http`) |
| Module | `http` (Package: `io.github.biezhi.java11.http`) |

## 1. Role

### Example.downloadFile()

This method demonstrates how to use the Java 11 `java.net.http.HttpClient` API to download the HTTP response body of a remote resource and save it directly to a file on the local filesystem. Specifically, it issues an HTTP GET request to `https://labs.consol.de/` (a public demonstration website operated by Consol Labs) and writes the response payload to a temporary file with a `.html` extension. It implements the **download-then-inspect** pattern: after retrieving the response, it prints the status code and the resulting file path to standard output so the caller can verify success and locate the downloaded content. As a static example method within the `java11.http.Example` class, its role in the larger system is purely educational — it serves as a runnable reference implementation for Java 11's new HTTP Client API and is not invoked by any other application logic.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["downloadFile()"])
    STEP1["Create HttpClient via newHttpClient()"]
    STEP2["Build HttpRequest with URI https://labs.consol.de/ and GET method"]
    STEP3["Create temp file via Files.createTempFile with prefix consol-labs-home and suffix .html"]
    STEP4["Send GET request via client.send(request, BodyHandlers.ofFile(tempFile))"]
    STEP5["Print response statusCode to stdout"]
    STEP6["Print response body (temp file path) to stdout"]
    END_NODE(["End"])

    START --> STEP1
    STEP1 --> STEP2
    STEP2 --> STEP3
    STEP3 --> STEP4
    STEP4 --> STEP5
    STEP5 --> STEP6
    STEP6 --> END_NODE
```

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method takes no parameters; the target URL, HTTP method, and file naming convention are all hardcoded constants within the method body. |

## 4. CRUD Operations / Called Services

This method does not perform any database CRUD operations or invoke any service components (SC/CBS). It exclusively uses the Java 11 `java.net.http` API to issue an HTTP request and save the response body to a temporary file. All operations are local filesystem writes and network I/O.

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| (none) | `HttpClient.send()` | — | — | Sends an HTTP GET request and streams the response body to a temporary file on disk |
| (none) | `Files.createTempFile()` | — | — | Creates a temporary file in the OS default temp directory with a unique name derived from the given prefix and suffix |

## 5. Dependency Trace

This method is not called by any other code in the codebase. It is a standalone example/demo method invoked only manually (e.g., from a `main` method or direct IDE run).

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | (none) — standalone example | `Example.main()` (manual invocation) | `client.send() [no DB]` → temp file |

## 6. Per-Branch Detail Blocks

> The method contains no conditional branches (no if/else, switch, loops, or try-catch). It executes a single linear sequence of operations.

**Block 1** — [LINEAR EXECUTION] `(no condition)` (L88–L100)

> Creates an HTTP client, builds a GET request to the target URL, creates a temporary file, sends the request to stream the response body into that file, then prints the HTTP status code and the resulting file path.

| # | Type | Code |
|---|------|------|
| 1 | CALL | `HttpClient.newHttpClient()` // Create a default HTTP client instance [-> `HttpClient` with default configuration (no proxy, no TLS settings)] |
| 2 | CALL | `HttpRequest.newBuilder().uri(new URI("https://labs.consol.de/")).GET().build()` // Build an HTTP GET request to the target URL [-> URI hardcoded as `https://labs.consol.de/` (Consol Labs demo site)] |
| 3 | CALL | `Files.createTempFile("consol-labs-home", ".html")` // Create a temporary file with a unique system-generated name [-> prefix=`consol-labs-home`, suffix=`.html`] |
| 4 | CALL | `client.send(request, HttpResponse.BodyHandlers.ofFile(tempFile))` // Send the HTTP request and stream the response body directly into the temp file [-> returns `HttpResponse<Path>` containing the response metadata and file path] |
| 5 | EXEC | `System.out.println(response.statusCode())` // Print the HTTP status code to standard output (e.g., `200`) |
| 6 | EXEC | `System.out.println(response.body())` // Print the resolved file path to standard output (the `Path` returned from `ofFile`) |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `HttpClient` | Class | Java 11 HTTP Client — the new standard-blocking HTTP client provided by the `java.net.http` package, used to send HTTP requests and receive responses |
| `HttpRequest` | Class | HTTP Request object representing an outbound HTTP request with method, headers, URI, and body |
| `HttpResponse<Path>` | Class | Response object wrapping the HTTP response where the body is handled as a file `Path` on the local filesystem |
| `BodyHandlers.ofFile(Path)` | API | Response body handler that writes the response body stream directly to the specified file on disk |
| `Files.createTempFile()` | API | Java NIO utility method that creates an empty temporary file with a unique name in the system's default temporary-file directory |
| `https://labs.consol.de/` | URI | Target URL — the Consol Labs demonstration website; used here as a public, always-available endpoint for example code |
| `consol-labs-home` | Constant | Prefix string used for the temporary file name; ensures all temp files created by this example are identifiable |
| `.html` | Constant | File suffix assigned to the downloaded content, indicating the target resource is an HTML page |
| `throws Exception` | Declaration | Method-level exception declaration — this static example propagates all checked exceptions (`URISyntaxException`, `IOException`, `InterruptedException`) to the caller |
