报错场景:
使用SSM框架实现文件上传时报“Failed to instantiate [org.springframework.web.multipart.MultipartFile]”错,控制器源代码:
@Controller @RequestMapping("/file") public class FileUDController { @RequestMapping(value="/fileUpload",method=RequestMethod.POST) public ModelAndView fileUpload(MultipartFile file,HttpServletRequest req) { String path = req.getSession().getServletContext().getRealPath("upload"); String fileName = file.getOriginalFilename(); File dir = new File(path,fileName); if(!dir.exists()) { dir.mkdirs(); } try { file.transferTo(dir); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ModelAndView mv = new ModelAndView("success");

在使用SSM框架上传文件时遇到'Failed to instantiate [org.springframework.web.multipart.MultipartFile]'错误。报错源于直接使用MultipartFile作为控制器参数导致Spring无法自动映射。解决方法是在参数前添加@RequestParam注解,例如:@RequestParam MultipartFile file,以正确映射上传文件。
最低0.47元/天 解锁文章
672

被折叠的 条评论
为什么被折叠?



