1、不用注解,可以自动接收到url中的参数
2、@RequestBody,只能接收post中的content-type为application/json的参数,注解只能有一个
3、@RequestParam,可以接收form-data、x-www-form-urlencoded,注解可以有多个
4、@RequestParam(“fileName”) MultipartFile file,接收上传的文件。如果有多个文件@RequestParam(“fileName”)List files
5、从HttpServletRequest request中取值,如果既有多个上传文件,又有多个参数需要接收,最好直接从request中取值,但是相对麻烦。接收文件首先要准备文件解析器:
@Configuration
public class MultipartResolverConfig {
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver(){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
resolver.setResolveLazily(true);//resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
resolver.setMaxInMemorySize(40960);
resolver.setMaxUploadSize(500*1024*1024);//上传文件大小 50M 50*1024*1024
return resolver;
}
}
从request中接收参数
@ResponseBody
@RequestMapping(value = "/initDeviceSoft", method = RequestMethod.POST, produces = "application/json")
public BaseResponse initDeviceSoft(HttpServletRequest request) throws ServiceException {
MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
String softName = params.getParameter("softName");
String softVersion = params.getParameter("softVersion");
List<MultipartFile> updateFile = ((MultipartHttpServletRequest) request).getFiles("update_zip");
List<MultipartFile> completeFile = ((MultipartHttpServletRequest) request).getFiles("complete_zip");
List<MultipartFile> md5File = ((MultipartHttpServletRequest) request).getFiles("md5");
}