How do I upload a file using multipart with Java 11 HttpClient?

To upload a file using the modern HttpClient introduced in Java 11, you can use the multipart/form-data request. The steps involve creating a HttpRequest and sending the file as part of the multipart request body. Below is an example implementation:

Example Code: File Upload with Multipart

package org.kodejava.net.http;

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 java.nio.file.Paths;
import java.util.UUID;

public class FileUploadDemo {
    public static void main(String[] args) throws Exception {
        // File to be uploaded
        Path filePath = Paths.get("path/to/your/file.txt");

        // Create boundary for multipart request
        String boundary = UUID.randomUUID().toString();

        // Build the multipart body
        String body = 
            "--" + boundary + "\r\n" +
            "Content-Disposition: form-data; name=\"file\"; filename=\"" + filePath.getFileName() + "\"\r\n" +
            "Content-Type: text/plain\r\n\r\n" +
            java.nio.file.Files.readString(filePath) + "\r\n" +
            "--" + boundary + "--\r\n";

        // Create HttpClient
        HttpClient client = HttpClient.newHttpClient();

        // Build HttpRequest
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://your-api-endpoint.com/upload"))
                .header("Content-Type", "multipart/form-data; boundary=" + boundary)
                .POST(HttpRequest.BodyPublishers.ofString(body))
                .build();

        // Send the request and handle response
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        // Print response
        System.out.println("Response Code: " + response.statusCode());
        System.out.println("Response Body: " + response.body());
    }
}

Explanation:

  1. Boundary:
    • A boundary string (UUID) is specified to separate parts in the multipart/form-data content. It’s used in headers and to indicate where each part starts and ends.
  2. Request Body:
    • Each part in the multipart body includes:
      • Content-Disposition: Specifies form-data, name, and file name.
      • Content-Type: MIME type of the file (e.g., text/plain for text files).
      • File content is appended after the headers.
  3. Headers:
    • Set the Content-Type header to multipart/form-data with the boundary value.
  4. HttpClient:
    • HttpClient sends the HTTP POST request, and BodyPublishers.ofString() sets the request body.

Notes:

  • Replace "path/to/your/file.txt" with the actual path of the file to upload.
  • Update "https://your-api-endpoint.com/upload" with the appropriate API endpoint for uploading files.
  • Adjust the Content-Type for the file if it’s not a plain text file (e.g., "image/png", "application/json").

Considerations for Larger Files:

The above example uses files.readString() to read the file content into memory. For large files, this approach may corrupt memory. Instead, you can use BodyPublishers.ofFile() to stream the file content directly:

HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofFile(filePath);
// Use this body publisher in your POST request

This is more memory-efficient and suitable for uploading large files.

How do I do multipart upload using HttpClient?

This example demonstrates how to do multipart upload using the Apache HttpClient library. In this example we upload a single file. We start by creating an object of the file to be uploaded. The FileBody represent the binary body part of the file.

Next, prepare the HttpEntity object by create an instance of MultipartEntityBuilder. Add parts to this object, in this case we add the fileBody. We can add multiple part to this object as the name says. It can be string, file, etc. as we do in a normal web form.

The build() method of the builder object finalize the entity creation and return us the HttpEntity object. To send / upload to server we create an HttpPost request and set the entity to be posted. Finally, the execute() method of the HttpClient object send the multipart object to server.

package org.kodejava.apache.http;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.File;
import java.io.IOException;

public class HttpPostMultipartExample {
    public static void main(String[] args) {
        try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
            File file = new File("data.zip");
            FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            builder.addPart("file", fileBody);
            HttpEntity entity = builder.build();

            HttpPost request = new HttpPost("http://localhost:8080/upload");
            request.setEntity(entity);
            client.execute(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

To receive the file on the server you can take a look at the servlet code in the following example: How do I create a web based file upload?.

Maven Dependencies

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.14</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.14</version>
    </dependency>
</dependencies>

Maven Central Maven Central