springMVC在使用ajax进行后台传值的时候发现找不到对应的requestMapping(""),无法进入后台,在多次试验后确定是
MultipartFile对象与ajax冲突;并且不报任何异常,直接调用ajax的失败属性方法处理;
代码如下:
·1 @RequestMapping("/addimage")
2 public String addimage(@RequestParam("image")MultipartFile file,HttpServletRequest request){
3
4 PersonMsgService.setEntityClass(Personalmsg.class);
5 Personalmsg bean=(Personalmsg) PersonMsgService.get(getInt(request, "personMsgID"));
6
7 String fileName =getInt(request, "personMsgID")+".JPG";
8 System.out.println(fileName);
9
10 String path = request.getSession().getServletContext().getRealPath("upload");
11 System.out.println(path);
12 File targetFile = new File(path,fileName);
13 if(!targetFile.exists()){
14 targetFile.mkdirs();
15 }
16 try {
17 file.transferTo(targetFile);
18 bean.setImage(path+"\"+fileName);
19 System.out.println(bean);
20 PersonMsgService.saveOrUpdate(bean);
21 } catch (IllegalStateException e) {
22 e.printStackTrace();
23 } catch (IOException e) {
24 e.printStackTrace();
25 }
26 return "redirect:/test/PersonMsg/tolist.spring";
27 }
如图
@RequestParam("image")MultipartFile file这个参数会导致按键按下ajax无法匹配对应的@RequestMapping("/addimage");
使用
MultipartRequest mrq=(MultipartRequest) request;对request转换也无法成功
当前我的解决方法是放弃使用ajax直接使用form表单提交,form中,设置method=post ,enctype=”multipart/form-data”
;