@ControllerAdvice
public class MulitpartExceptionHandler {
@ExceptionHandler(MultipartException.class)
public String handleError(MultipartException e, RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("error", e.getCause().getMessage());
return "redirect:/uploadForm";
}
}
@GetMapping("/uploadForm")
public String uploadForm() {
return "upload";
}
@PostMapping("/uploadFile")
public String uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request, Model model)
throws IOException {
String rootDirectory = request.getSession().getServletContext().getRealPath("/");
Path path = Paths.get(rootDirectory + fileDirectory + file.getOriginalFilename());
try {
file.transferTo(new File(path.toString()));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("File saving failed", e);
}
model.addAttribute("filename", file.getOriginalFilename());
return "success";
}
spring mvc 上传文件过大,上面的重定向无效,报异常,请问一下是什么原因,谢谢。
项目代码这里可以下载