Search code examples
spring-bootkotlinspring-resttornadofx

How to send multipart file to springboot server using tornadoFx?


Here can the following funcationality be acheived with TornadoFX. (Sorry for example in java)

<form method = "post" action="/store" enctype="multipart/form-data">
 <label>Image</label>
 <input type="file" name="imageFile" />
</form>           

and on controller side

@PostMapping
public String store(@Valid Item item, @RequestParam("imageFile") MultipartFile file) throws IOException {
 if (file != null) {

    Path path = Paths.get(System.getProperty("user.dir") + "/images/" + file.getOriginalFilename());
    Files.write(path, file.getBytes());
    }

    repository.save(course);
    model.addAttribute("success", "Item saved successfully");
    model.addAttribute("item", new Item());
    return "redirect:/items/form";
}

I did not find any example, so any small example would be very helpful. or if there is any other way to acheive this functionality? regards


Solution

  • Though you can manipulate the Rest client in TornadoFX to do this, it's easier to use Apache HttpClient directly, since it's already on your classpath and provides proper interfaces for sending multipart/form-data. You'll find plenty of HttpClient usage examples for multipart/form-data here on SO :)