如何将“边界”设置为java。网http。HttpRequest头
我试图使用JDK11中的HttpClient和HttpRequest向服务器发送一个多部分文件:
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();
}
服务器终结点:
@PostMapping("/api/logs/throw")
public void handleFileUpload(@RequestParam("logs.7z") MultipartFile file) {
Path path = Paths.get("/home/terminal_logs/");
storageService.store(file, path);
}
服务器端发生以下错误:
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
共 (0) 个答案