1

I have the following upload controller which has two params with DIFFERENT type: 1 is for the path the file will be saved to and 2 the file itself. I'm looking for the correct method definition instead of 2 @Requestparam which give error in STS.

@PostMapping("/{path}/")
public String handleFileUpload(@RequestParam("path"), @RequestParam("file") MultipartFile file,
        RedirectAttributes redirectAttributes) {
    
    filesStorageService.store(file);
    redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!");
    
    return "redirect:/";
}
1
  • for path you can use @PathVariable("path") String path as parameter Commented Jul 6, 2021 at 5:39

1 Answer 1

7

You need to use the @PathVariable annotation for the path parameter and add an additional argument (String path) to store it:

@PostMapping("/{path}/")
public String handleFileUpload(
   @PathVariable("path") String path,
   @RequestParam("file") MultipartFile file,
   RedirectAttributes redirectAttributes) {
   [...]
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.