Close

Spring MVC - MultipartFile Examples

[Last Updated: Nov 9, 2025]

Spring MVC 

    @RequestMapping(method = RequestMethod.POST)
public String handlePost(@RequestParam("user-file")
MultipartFile multipartFile,
Model model) throws IOException {

String name = multipartFile.getOriginalFilename();
Path path = Paths.get("d:\\filesUploaded\\" + name);
Path parent = path.getParent();
if (!Files.exists(parent)) {
Files.createDirectory(parent);
} else if (Files.exists(path)) {
Files.delete(path);
}
BufferedWriter w = Files.newBufferedWriter(path);
w.write(new String(multipartFile.getBytes()));
w.flush();


model.addAttribute("msg", "File has been uploaded. name: " + name+
" content: "+new String(multipartFile.getBytes()));
return "response";
}
Original Post




See Also

Join