I am trying to build a file-uploader with Java/Spring-boot/Vaadin.
https://vaadin.com/components/vaadin-upload/java-examples
That Uploader gives me a MemoryBuffer. How can I post this to my backend, which looks like this:
@RestController
@RequestMapping("files")
public class FilesController {
...
@PostMapping
public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) {
try {
fileService.save(file);
return ResponseEntity.status(HttpStatus.OK)
.body(String.format("File uploaded successfully: %s", file.getOriginalFilename()));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(String.format("Could not upload the file: %s!", file.getOriginalFilename()));
}
}
...
I tried something like:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:9091/upload/files");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
but I don't know how to add the requested MultipartFiles. Thank you :)
I haven't tested this code, so it may or may not work. It's based on a Baeldung article and the FileData JavaDoc.