Spring:单个对象中的Multipart-Formdata

时间:2013-10-16 14:05:49

标签: spring spring-mvc file-upload

是否可以将多部分表单的内容捆绑到相应控制器方法中的单个对象中?

即。如何转换方法

@RequestMapping(value = "/mult", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<SomeBean> upload(@RequestBody MultipartFile multipartFile, @RequestParam String param)

进入具有以下签名的方法:

@RequestMapping(value = "/mult", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<SomeBean> upload(@RequestBody MultipartWrapper wrapper)

其中MultipartWrapper包含multipartFile和附加参数:

public class MultipartWrapper {
    private String param;
    private MultipartFile multipartFile;

    // Getters & Setters
}

1 个答案:

答案 0 :(得分:1)

在其他控制器中使用@ModelAttribute:

@RequestMapping(value = "/mult", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<SomeBean> upload(@ModelAttribute("cmd") MultipartWrapper wrapper) {
    ...
}

@ModelAttribute("cmd")
public MultipartWrapper getWrapper() {
  return new MultipartWrapper();
}

public class MultipartWrapper {
  private String param;
  private MultipartFile multipartFile;

  // Getters & Setters
}