I tried to use HttpClient and HttpRequest from JDK11 to send a Multipart file to the server:
public static int sendMultipartFile(String uri, Path filePath) {
HttpClient httpClient =`enter code here` HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(uri))
.POST(HttpRequest.BodyPublishers.ofFile(filePath))
.header("Content-Type", "multipart/form-data")
.setHeader("Boundary", "gc0p4Jq0M2Yt08jU534c0p")
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
return response.statusCode();
}
Server endpoint:
@PostMapping("/api/logs/throw")
public void handleFileUpload(@RequestParam("logs.7z") MultipartFile file) {
Path path = Paths.get("/home/terminal_logs/");
storageService.store(file, path);
}
Following error occurs in the server side:
ERROR 17072 --- [nio-8081-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause