'How to upload files using JDK 11 java.net.http.HttpClient?

I recently encountered some problems with java.net.http.HttpClient that comes with JDK 11. I don't know how to use file upload. Found the ofInputStream() in java.net.http.BodyPublishers. I don't know if I using this method file upload. Here are the examples I wrote.

    public HttpResponse<String> post(String url, Supplier<? extends InputStream> streamSupplier, String... headers) throws IOException, InterruptedException {
        HttpRequest.Builder builder = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .headers(headers)
                .POST(null == streamSupplier ?
                        HttpRequest.BodyPublishers.noBody() : HttpRequest.BodyPublishers.ofInputStream(streamSupplier));
        HttpRequest request = builder.build();
        log.debug("Execute HttpClient Method:『{}』, Url:『{}』", request.method(), request.uri().toString());
        return client.send(request, HttpResponse.BodyHandlers.ofString());
    }


Solution 1:[1]

The java.net.http.HttpClient handles bytes supplied through the BodyPublisher as raw body data, without any interpretation. Whether you use HttpRequest.BodyPublishers::ofFile(Path) or HttpRequest.BodyPublishers::ofByteArray(byte[]) is therefore semantically irrelevant: what changes is simply how the bytes that will be transmitted are obtained. In case of file upload - your server probably expects that the request body will be formatted in certain ways. It might also expect some specific headers to be transmitted with the request (such as Content-Type etc). The HttpClient will not do that magically for you. This is something you need to implement at the caller level.

Solution 2:[2]

The HttpRequest type provide factory method for creating request publisher for handling body type such as file:

HttpRequest.BodyPublishers::ofFile(Path)

You can update your method:

public HttpResponse<String> post(String url, Path file, String... headers) throws IOException, InterruptedException {
        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(url))
            .headers(headers)
            .POST(null == file ? HttpRequest.BodyPublishers.noBody() : 
                HttpRequest.BodyPublishers.ofFile(file))
            .build();

        log.debug("Execute HttpClient Method:?{}?, Url:?{}?", request.method(), 
            request.uri().toString());
        return client.send(request, HttpResponse.BodyHandlers.ofString());
}

Solution 3:[3]

you may use the method by:

public void uploadLocalFileToRemote(String notEncodedUrlStr, String remoteFilename, String localSourceDir, String localFilename) {
    
    Path sourcePath = Path.of(localSourceDir, localFilename);
    if(!sourcePath.toFile().canRead())
    {
        System.err.println("please check the local file existance/readability: " + sourcePath.toAbsolutePath());
        return;
    }
    
    FileInputStream ins = null;
    try {
        ins = new FileInputStream(sourcePath.toFile());//FileNotFoundException extends IOException
        BufferedInputStream buf_ins = new BufferedInputStream(ins);
        Supplier<? extends InputStream> streamSupplier = new Supplier<BufferedInputStream>() {
            @Override
            public BufferedInputStream get() {
                return buf_ins;
            }
        };
        
        //HttpResponse<String> response = post(notEncodedUrlStr, streamSupplier, 
        HttpResponse<String> response = post(notEncodedUrlStr, () -> buf_ins, 
                "User-Agent", "Java 11 HttpClient Bot", "Content-type", "application/octet-stream",
                "accept", "*/*", "fileName", remoteFilename);
        
        // print response:
        System.out.println(response.version().name() + " " + response.statusCode());
        // print response headers
        HttpHeaders headers = response.headers();
        headers.map().forEach((k, v) -> System.out.println(k + ":" + v));
        
        // print response body
        String body = response.body();
        System.out.println(body);
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

another consideration is how your server side is implemented. here assume the server side will using http 1.1 "chunked". and configured a directory for remoteFilename.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 donquih0te
Solution 3 qi shan