Java Spring File upload using webclient

47 Views Asked by At

I amtrying to upload a file using webclient in Java but seems not to work and keep getting errors. The code I tried:

    var file = new File(tempDirectory + File.separator + myFile);

    var multipartBodyBuilder = new MultipartBodyBuilder();
    //multipartBodyBuilder.part("ticket", ticket);
    multipartBodyBuilder.part("file", new FileSystemResource(file));
    
    var mimeMultipartData = multipartBodyBuilder.build();

    return webclient.post().uri(url).contentType(MediaType.MULTIPART_FORM_DATA)
            .body(BodyInserters.fromMultipartData(mimeMultipartData).with("ticket", ticket)).retrieve().bodyToMono(String.class);

}

I get an error "Error ticket is null" and if i add the "ticket" to multipartBodyBuilder then i get error "end of file never reached". Do i miss to configure something. Any guidance is appreciated. Thanks.

0

There are 0 best solutions below