1

My client upload method:

public static void addPhoto(File photo) throws ParseException, IOException, InterruptedException {
    HttpClient client = HttpClient.newBuilder().build();
    HttpRequest request = HttpRequest.newBuilder()
            .header("Content-Type","image/jpg")
            .uri(URI.create(baseUrl + "data/addPhoto?date=4&category=temp&jwt="+jwt))
            .PUT(HttpRequest.BodyPublishers.ofFile(photo.toPath()))
            .build();
    client.send(request, HttpResponse.BodyHandlers.ofString());
}

My Spring Boot method that receives the file:

@PutMapping(path = "/addPhoto")
public @ResponseBody
boolean addPhoto(@RequestParam(name = "jwt") String jwt,
                 @RequestParam("file") MultipartFile file,
                 @RequestParam(name = "date") long date,
                 @RequestParam(name = "category") String category) {
    return crudService.addPhoto(jwt, date, file, category);
}

The current error:

2020-09-17 16:29:02.313 ERROR 8636 --- [nio-5000-exec-9] 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: Current request is not a multipart request] with root cause

What kind of headers can I add to ensure my Spring Boot server receives the file without errors?

2
  • Does this answer your question? How can I make a multipart/form-data POST request using Java? Commented Sep 17, 2020 at 23:48
  • No it does not, that is the Apache Http Library and I am using the new native Java HttpClient. Thank you though! Commented Sep 17, 2020 at 23:53

1 Answer 1

3

MultipartException: Current request is not a multipart request

This is telling you what's wrong.

In your code: .PUT(HttpRequest.BodyPublishers.ofFile(photo.toPath())), you are doing a PUT request with file's byte array in BODY. But in your server, you are expecting it as MultipartFile. MultipartFile is a representation of uploaded file with additional data in the POST request body. https://en.wikipedia.org/wiki/MIME#Multipart_messages

You can simply do the following to upload your file:

Ref: https://ganeshtiwaridotcomdotnp.blogspot.com/2020/09/java-httpclient-tutorial-with-file.html

Send filename in request:

.uri(URI.create("http://localhost:8085/addPhoto?fileName=" + photo.getName()))

Receive byte array in RequestBody and fileName in RequestParam

@PostMapping(path = "/addPhoto")
public void addPhoto(@RequestBody byte[] barr,
                     @RequestParam(name = "fileName") String fileName) throws Exception {
    System.out.println(" received file " + fileName + " length " + barr.length);

    try (OutputStream os = new FileOutputStream(new File("UPL" + fileName))) {
        os.write(barr);
    }

}

If you must use MultipartFile then you can do sth similar to:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.