1 
 2 import java.net.URI;
 3 import java.net.http.HttpClient;
 4 import java.net.http.HttpRequest;
 5 import java.net.http.HttpResponse.BodyHandlers;
 6 import java.nio.file.Path;
 7 
 8 public class MultipartBodyPublisherRawTest {
 9 
10     static final String CRLF = "\r\n";
11     static final String BOUNDARY = "gc0p4Jq0M2Yt08jU534c0p";
12     static final String BOUNDARY_START = "--";
13     static final String BOUNDARY_END = "--";
14 
15     public static void main(String... args) throws Exception {
16 
17         // # using raw concat only !
18         var publisher = HttpRequest.BodyPublishers.concat(
19                 // preamble
20                 HttpRequest.BodyPublishers.ofString(CRLF + BOUNDARY_START + BOUNDARY + CRLF + "Content-Type: text/plain; charset=us-ascii" + CRLF),
21                 HttpRequest.BodyPublishers.ofFile(Path.of("/tmp/xxyy")),
22                 HttpRequest.BodyPublishers.ofString(CRLF + BOUNDARY_START + BOUNDARY + BOUNDARY_END)
23                 // epilogue
24         );
25 
26         var client = HttpClient.newHttpClient();
27         var request = HttpRequest.newBuilder()
28                 .uri(URI.create("http://foo.com/"))
29                 .header("Content-Type", "multipart/mixed; boundary=" + BOUNDARY)
30                 .POST(publisher)
31                 .build();
32         client.send(request, BodyHandlers.ofString());
33     }
34 }